473,400 Members | 2,145 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,400 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 1484
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.