473,224 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 1482
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
1
by: jimfortune | last post by:
The following Access VBA function creates a string that can be used in a pdf stream to draw or fill a rectangle of a given color with rounded corners: 'Begin code----------- Public Function...
3
by: V. Jenks | last post by:
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"...
17
by: Dooglo | last post by:
I've forgotten. How do I round off numbers from an equation or round off to the third decimal point for instance. eg. answers number is 12345.678910
1
by: serge calderara | last post by:
Dear all Any idea to how to make a panel with rounded corner ? regards serge
5
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object...
0
by: BuckBaxter | last post by:
I'm trying to calculate the ExtendedPrice field. code: = * UnitPrice and ExtendedPrice are currency data types. Is there a way to get the real result, rather than a rounded off one?
1
by: sconnors | last post by:
I am currently working on an application that runs on Windows XP SP2 with .NET 2.0, Linux 2.4.19-16mdkenterprise and Solaris 5.9. The language is C++. When I compare the output numbers, Windows...
1
by: kidelectric | last post by:
The issue I am having is that I would like to be able to drag-and-drop div elements that have rounded corners.* Since these elements will be dynamically created (including background color), I could...
8
by: rfox | last post by:
Searching the web, there seems to be a myriad of ways to achieve rounded corners on a fixed-width div. I'm looking for suggestions on the simplest, most straight-forward method of achieving this. ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.