cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Visitor
Visitor
419 Views
Registered: ‎11-12-2020

Rounding a number to 2 digits after point

Jump to solution

Hello, 

I would like to know how I can round a number to only 2 digits after point. for example: 

round (4.2564,2) = 4.25 

Thanks

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Teacher
Teacher
196 Views
Registered: ‎07-09-2009
binary rounding.

take a unsigned number in base 2, say

11101010101

first question you need to ask, is where is the binary point.

assume its at the third bit,
so you could think of this as 111.01010101

to round this off to the "integer"part only
just add 0.100000 and truncate.

11101010101 + 00010000000 = 11111010101
take the top three bits, gives 111 as expected.

If you want to round off to two binary places,
add 00000100000

11101010101 + 00000100000 = 11101110101

take the 5 bits gives
111.01 as expected



<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>

View solution in original post

10 Replies
Highlighted
Instructor
Instructor
397 Views
Registered: ‎10-23-2018

@Mor_Yag 

are you really wanting 'round' or just truncation? ... your example looks more like truncation me.

there are library functions for this, depending in the tech (HLS, ...) you are using, but to truncate, you can simply multiple by 100, get the integer portion, then divide by 100. If you want to round, and half of the precision at the level you want to round first. (e.g. .005), then do multiply/integer/divide.

Hope that Helps
If so, Please mark as solution accepted. Kudos also welcomed.

Highlighted
Teacher
Teacher
371 Views
Registered: ‎07-09-2009
Are you working in decimal or binary ?
<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
Highlighted
Visitor
Visitor
360 Views
Registered: ‎11-12-2020

Hey,

Unfortunately I didn't understand, I want to implement the round function of matlab.

Y = round(X,N) rounds to N digits:

  • N > 0: round to N digits to the right of the decimal point.

  • N = 0: round to the nearest integer.

  • N < 0: round to N digits to the left of the decimal point

I need it for the row here with round function https://www.geeksforgeeks.org/draw-seychelles-flag-using-matlab/ 
(Im working in decimal, just like in the link above)

 
Thanks for helping!

0 Kudos
Highlighted
Instructor
Instructor
346 Views
Registered: ‎10-23-2018

@Mor_Yag 

Let me give you a clue, rather than the answer...

float myRound(float x) {        
int iv;

  if (x < 0.0)

  iv = (x - 0.005);

  else

  iv = (x + 0.005);

  return iv / 100.0;

}

 The value of 0.005 and 100 would depend on the value of N, so, rather than being hardcoded, you would create variables based on the value N.

This would allow you to create a general purpose function to round... If you don't need negatives, you can simplify it... if you don't need -N values, you can simplify it further.

Hope that Helps
If so, Please mark as solution accepted. Kudos also welcomed.

0 Kudos
Highlighted
Teacher
Teacher
253 Views
Registered: ‎07-09-2009
@Mor_Yag

your posting in the FPGA synthesis forum,
FPGAs are normally binary, 1 and 0 .

Your example is I guess in base 10,
so are you taking a base 10 set of instructions, which I guess are designed for a computer, and put them into an FPGA ?

or is this just a Matlab exercise.



<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
Highlighted
Voyager
Voyager
246 Views
Registered: ‎05-11-2015

 

In 4.2564,2

- If the dot is a decimal point, what is the colon?

- If the colon is the decimal point, what is the dot four digits apart?

Rounding a number to X decimal places is one thing, rounding a number to X significant figures is another thing.

---------------------------------------------------------------------------------------------------
Have you tried upgrading the operating system of your spirit level?
-------------------------------------------------------------------------------------------------
0 Kudos
Highlighted
Teacher
Teacher
235 Views
Registered: ‎07-09-2009
For interest,
there are many kinds of rounding !!

this is a good little read ( IMHO ) of how this small thing can catch out even the big systems
https://cran.r-project.org/web/packages/round/vignettes/Rounding.html
<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
Highlighted
Scholar
Scholar
224 Views
Registered: ‎08-07-2014

I *guess* the OP wants to implement a Matlab round-up function in HDL. He probably understands how to do it in C or Matlab (or he just uses a library) but has no idea how to do such a thing with Verilog or VHDL.

@Mor_Yag ,

Is that so?

------------FPGA enthusiast------------
Consider giving "Kudos" if you like my answer. Please mark my post "Accept as solution" if my answer has solved your problem

Highlighted
Visitor
Visitor
209 Views
Registered: ‎11-12-2020
Yes, that's right
0 Kudos
Highlighted
Teacher
Teacher
197 Views
Registered: ‎07-09-2009
binary rounding.

take a unsigned number in base 2, say

11101010101

first question you need to ask, is where is the binary point.

assume its at the third bit,
so you could think of this as 111.01010101

to round this off to the "integer"part only
just add 0.100000 and truncate.

11101010101 + 00010000000 = 11111010101
take the top three bits, gives 111 as expected.

If you want to round off to two binary places,
add 00000100000

11101010101 + 00000100000 = 11101110101

take the 5 bits gives
111.01 as expected



<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>

View solution in original post