formatting numbers

DiverDan

Contributor
Joined
Jan 16, 2003
Messages
645
Location
Sacramento, CA
Is there a way to format a number with comma dividers and still maintain the full decimal remainder? Breaking the number apart , formating the whole number portion then adding back the decimal portion will not work in this case.

Thanks
 
Have you tried to pass a NumberFormatInfo string to the toString() method of the number in question? Did you want a custom format that's not provided?
 
Here's what I have currently:

Visual Basic:
            Dim ValueOutDecimalPart As Double
            Dim DecimalPart As String
            If ValueOut < 1.0E+17 Then
                ValueOutDecimalPart = ValueOut - Fix(ValueOut)
                DecimalPart = ValueOutDecimalPart.ToString
                DecimalPart = DecimalPart.Remove(0, 1)
                tbValueOut.Text = Format(Fix(ValueOut), "##,##0") & DecimalPart
            Else
                tbValueOut.Text = ValueOut.ToString
            End If

It seems like a strange way of getting commas into numbers and I was hoping that I missed something about number formatting and could format the ValueOut number as:

Format(ValueOut,"commas only and leave the decimal portion alone").

Any Ideas?

Dan
 
Last edited by a moderator:
Why not use the ToString method, as quwiltw suggested?

You can use it like this:
Visual Basic:
Dim ValueOutDecimalPart As Double
ValueOutDecimalPart = 123456.789
Debug.WriteLine(ValueOutDecimalPart.ToString("n"))
'Displays "123,456.79"

There are other options on the ToString method - check out the help for more info.

-Nerseus
 
Yes, I understand, however the "n" format rounds the decimal to the second place. (Format(ValueOut,"n").ToString also returns the same result). I'm trying to keep as many of the decimal places intact with rounding only occuring with extremely large (astronomical units) numbers. My listed method will return:

25,899,881,103,360.0546875 prior to any decimal place rounding or truncating. With smaller numbers the decimal place will become more accurate. I understand that engineers can be a real pain sometimes :-) but in this case it is important to keep as accurate as possible. Formatting "##,##0.0000000 also works but it leaves a fixed decimal point lenght.

I was hoping that I had missed something in number formatting.

Thanks
Dan
 
Hi Iceplug, yes, I've tried formatting this way but it returns a fixed decimal point. The idea is to display the whole numbers with comma seperation and still retain a floating decimal point that can be of any lenght, ie 0 to 15 places depending on the calculation. Also to display an exponential number when the calculated number becomes extremely large.

I've succcessfully used my method while using a scripting language. I was hoping that there was a better way of accomplishing comma seperation and non-fixed decimal places in VB.

Thanks
Dan
 
Hi Derek,

Yes, you missed something. The idea is to keep a true floating, not fixed, decimal place while displaying comma seperated numbers. It is used in a conversion calculator and the output numbers will range in size and decimal accuracy. If I use your format, Astronomical numbers will stretch across the screen with almost no end (this is the reason to set a maximum value on the comma formatting so that an exponential can be used instead) and regular whole numbers will end up with #??? fixed decimal places instead of none or one and extremely small numbers will be displayed as a 0.0000?? format of zero instead of 0.00000000000213 or whatever the resulting calculation is. An AMU (atomic mass unit) or nano number will result in a very small number or visa versa depending on the query.

This is a printout example: 1,256.637085848544984
another example is: 5
and another is 5.8156E+64

Please keep in mind that 5.00 is not the same as 5 because of its significant figures.

Infact, looking at my code, I need to also set a minimum limit so a true negitave exponential can also be displayed.

I was wondering if VB had a format that accomindated for this extreme flux in number ranges without having to seperate the number, format part of it, and then put it back together and still accommodate for an extremely large or small number with an exponental format.

And I guess I haven't explained it clearly (as happens often to engineers) but thank you all for your input and help.

Dan
 
Last edited:
Back
Top