Add thousand separator and keep decimal precision
-
Hi, I have a numer in a string that may or may not have a decimal delimiter and some numbers afterwards, e.g. 1234 or 1234,5 or 1234,567. Now I want to show this number to the user with thousand separators, and keep the decimal precision from the original number. I.e. the number should be 1.234 and 1.234,5 and 1.234,567 If I use the following code
NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalSeparator = ","; nfi.NumberGroupSeparator = "."; double.Parse(m_value).ToString("N",nfi);
I'll get 1.234,00 and 1.234,50 and 1.234,57 - which is not what I want. Is there any other way to just add the thousand seperators and leave the decimal things unchanged ? It seems that nfi.NumberDecimalDigits misses a "dont mess with it" number... Any ideas ? /Jan Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert -
Hi, I have a numer in a string that may or may not have a decimal delimiter and some numbers afterwards, e.g. 1234 or 1234,5 or 1234,567. Now I want to show this number to the user with thousand separators, and keep the decimal precision from the original number. I.e. the number should be 1.234 and 1.234,5 and 1.234,567 If I use the following code
NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalSeparator = ","; nfi.NumberGroupSeparator = "."; double.Parse(m_value).ToString("N",nfi);
I'll get 1.234,00 and 1.234,50 and 1.234,57 - which is not what I want. Is there any other way to just add the thousand seperators and leave the decimal things unchanged ? It seems that nfi.NumberDecimalDigits misses a "dont mess with it" number... Any ideas ? /Jan Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concertIf I understand you question, then I think you've got your nfi.NumberDecimalSeperator and your ndi.NumberGroupSeperator contents backwards. Try switching the two assignments around and see if that helps.
-
Hi, I have a numer in a string that may or may not have a decimal delimiter and some numbers afterwards, e.g. 1234 or 1234,5 or 1234,567. Now I want to show this number to the user with thousand separators, and keep the decimal precision from the original number. I.e. the number should be 1.234 and 1.234,5 and 1.234,567 If I use the following code
NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalSeparator = ","; nfi.NumberGroupSeparator = "."; double.Parse(m_value).ToString("N",nfi);
I'll get 1.234,00 and 1.234,50 and 1.234,57 - which is not what I want. Is there any other way to just add the thousand seperators and leave the decimal things unchanged ? It seems that nfi.NumberDecimalDigits misses a "dont mess with it" number... Any ideas ? /Jan Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concertYou could use something like that ... string m_value = "12347890123456789,57"; string strhi = m_value; string strlow = ""; int DecLocation = m_value.IndexOf(","); int StrLen = m_value.Length; if ( DecLocation >=0 ) { strhi = m_value.Remove(DecLocation,StrLen-DecLocation); strlow = m_value.Substring(DecLocation+1,StrLen-DecLocation-1); m_value = Separ(strhi)+","+strlow; } else m_value = Separ(m_value); private string Separ(string str) { int i = str.Length/3; int maxlen = str.Length; if ( i*3 == maxlen) i--; string newstr = str; for(int j=1;j<=i;j++) newstr=newstr.Insert(maxlen-j*3,"."); return newstr; }