Numbers being rounded off!

V

V. Jenks

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
 
M

Michael Giagnocavo [MVP]

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
 
G

Guest

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
 
M

Morten Wennevik

Could you show a small but complete sample code that demonstrates your
error?

Happy coding!
Morten Wennevik [C# MVP]
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top