Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to round to specified number of decimals?

1,933 views
Skip to first unread message

saneman

unread,
Jan 16, 2008, 7:04:36 AM1/16/08
to
How do I round this number:

20.12453

to

20.1?

or 20.12

more generally how do you specify number of decimals when rounding?

saneman

unread,
Jan 16, 2008, 7:09:13 AM1/16/08
to

bob = roundn(20.12453,-1)

gives:

20.1000

but how do I delete the trailing zeros?

Yumnam Kirani Singh

unread,
Jan 16, 2008, 7:25:06 AM1/16/08
to
you see the help on format command using the following command
>> help format
You can also see the help on fprintf or sprintf.

Walter Roberson

unread,
Jan 16, 2008, 12:38:40 PM1/16/08
to
In article <fmkrs4$mi3$1...@news.net.uni-c.dk>, saneman <yy...@dd.com> wrote:

>How do I round this number:

>20.12453

>to

>20.1?

>or 20.12

You cannot. There is no exact binary floating point representation
for 20.1 or 20.12, so no matter what you do, you will not be able
to round to exactly the target value. For numbers in the range of 20.1
the closest you can get will be off by about 2^(-49).

If you need to limit the number of decimal places for string output
purposes, then you can use an output format such as %.2f

--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes

0 new messages