Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Community Forums
- :
- Forums
- :
- Vivado RTL Development
- :
- Synthesis
- :
- Re: Rounding a number to 2 digits after point

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted

Mor_Yag

Visitor

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-12-2020 09:26 AM

419 Views

Registered:
11-12-2020

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

1 Solution

Accepted Solutions

Highlighted
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

drjohnsmith

Teacher

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2020 05:43 AM

196 Views

Registered:
07-09-2009

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 ==>

10 Replies

Highlighted

xilinxacct

Instructor

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-12-2020 10:03 AM

397 Views

Registered:
10-23-2018

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
Are you working in decimal or binary ?

drjohnsmith

Teacher

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-12-2020 11:06 AM

371 Views

Registered:
07-09-2009

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

Highlighted

Mor_Yag

Visitor

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-12-2020 11:32 AM - edited 11-12-2020 11:35 AM

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!

Highlighted

xilinxacct

Instructor

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-12-2020 11:47 AM - edited 11-12-2020 11:54 AM

346 Views

Registered:
10-23-2018

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

`float`

`myR`

`ound(`

`float`

`x)`

`{`

`int iv;`

`if (x < 0.0)`

`iv =`

`(x - 0.`

`00`

`5);`

`else`

`iv =`

`(x + 0.`

`00`

`5);`

`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.

Highlighted
@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.

drjohnsmith

Teacher

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2020 03:52 AM

253 Views

Registered:
07-09-2009

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

joancab

Voyager

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2020 04:45 AM

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?

-------------------------------------------------------------------------------------------------

Have you tried upgrading the operating system of your spirit level?

-------------------------------------------------------------------------------------------------

Highlighted
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

drjohnsmith

Teacher

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2020 05:04 AM

235 Views

Registered:
07-09-2009

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 ==>

Highlighted

dpaul24

Scholar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2020 05:23 AM - edited 11-13-2020 05:26 AM

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

Mor_Yag

Visitor

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2020 05:28 AM

209 Views

Registered:
11-12-2020

Yes, that's right

Highlighted
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

drjohnsmith

Teacher

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2020 05:43 AM

197 Views

Registered:
07-09-2009

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 ==>