*Experts* DiverDan Posted March 6, 2003 *Experts* Posted March 6, 2003 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 Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Leaders quwiltw Posted March 6, 2003 Leaders Posted March 6, 2003 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? Quote --tim
*Experts* DiverDan Posted March 6, 2003 Author *Experts* Posted March 6, 2003 (edited) Here's what I have currently: 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 Edited March 6, 2003 by divil Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* Nerseus Posted March 6, 2003 *Experts* Posted March 6, 2003 Why not use the ToString method, as quwiltw suggested? You can use it like this: 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Experts* DiverDan Posted March 7, 2003 Author *Experts* Posted March 7, 2003 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 Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Leaders Iceplug Posted March 7, 2003 Leaders Posted March 7, 2003 Guess Formatting "##,##0.0000000 also works but it leaves a fixed decimal point lenght. Hmm... have you tried "##,##0.##" or "##,##0.0##"? :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
*Experts* DiverDan Posted March 7, 2003 Author *Experts* Posted March 7, 2003 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 Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Gurus* Derek Stone Posted March 7, 2003 *Gurus* Posted March 7, 2003 .ToString("###,###.###############") Am I missing something? :) Quote Posting Guidelines
*Experts* DiverDan Posted March 8, 2003 Author *Experts* Posted March 8, 2003 (edited) 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 Edited March 8, 2003 by DiverDan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.