468,790 Members | 1,861 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,790 developers. It's quick & easy.

Numbers being rounded off!

I'm having a problem with the Convert.ToSingle and the
O:C formatting rounding off my floating-point numbers.

When I .ToString a float, it rounds the number. It also
rounds it if I use the "C" or "F" format strings (i.e.
num.ToString("C"))

How can I accurately display a float/currency value w/o
it being rounded off?

Thanks!

-v
Nov 16 '05 #1
4 1334
See the docs on System.Globalization.NumberFormatInfo:
r, R - Roundtrip format, which ensures that numbers converted
to strings will have the same value when they are converted back to numbers.

So if you use "R", then you can be sure the number will convert right back
into the number you have. For currency, don't use a float. Use a decimal.

-Michael
MVP

"V. Jenks" <sp**@scientifik.com> wrote in message
news:18*****************************@phx.gbl...
I'm having a problem with the Convert.ToSingle and the
O:C formatting rounding off my floating-point numbers.

When I .ToString a float, it rounds the number. It also
rounds it if I use the "C" or "F" format strings (i.e.
num.ToString("C"))

How can I accurately display a float/currency value w/o
it being rounded off?

Thanks!

-v


Nov 16 '05 #2
That didn't work at all.

Using .ToString("R") turned 10.99 into 11, for example.

I'm also looking at my data, it's definitely 10.99
there....but all I can manage to get out is a rounded
number, even using "R".

-v
-----Original Message-----
See the docs on System.Globalization.NumberFormatInfo:
r, R - Roundtrip format, which ensures that numbers convertedto strings will have the same value when they are converted back to numbers.
So if you use "R", then you can be sure the number will convert right backinto the number you have. For currency, don't use a float. Use a decimal.
-Michael
MVP

"V. Jenks" <sp**@scientifik.com> wrote in message
news:18*****************************@phx.gbl...
I'm having a problem with the Convert.ToSingle and the
O:C formatting rounding off my floating-point numbers.

When I .ToString a float, it rounds the number. It also rounds it if I use the "C" or "F" format strings (i.e.
num.ToString("C"))

How can I accurately display a float/currency value w/o
it being rounded off?

Thanks!

-v


.

Nov 16 '05 #3
Could you show a small but complete sample code that demonstrates your
error?

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
<an*******@discussions.microsoft.com> wrote:
That didn't work at all.

Using .ToString("R") turned 10.99 into 11, for example.

I'm also looking at my data, it's definitely 10.99
there....but all I can manage to get out is a rounded
number, even using "R".


I suspect you'll find that you've got a problem reading your data then.
The float closest to 10.99 certainly wouldn't be displayed as 11 using
the "R" specifier.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian van den Broek | last post: by
3 posts views Thread by V. Jenks | last post: by
17 posts views Thread by Dooglo | last post: by
1 post views Thread by serge calderara | last post: by
5 posts views Thread by soeren | last post: by
8 posts views Thread by rfox | last post: by
1 post views Thread by CARIGAR | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.