Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Add thousand separator and keep decimal precision

Add thousand separator and keep decimal precision

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan R Hansen
    wrote on last edited by
    #1

    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

    K M 2 Replies Last reply
    0
    • J Jan R Hansen

      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

      K Offline
      K Offline
      keith maddox
      wrote on last edited by
      #2

      If 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.

      1 Reply Last reply
      0
      • J Jan R Hansen

        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

        M Offline
        M Offline
        MBGeorge
        wrote on last edited by
        #3

        You 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; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups