Results 1 to 5 of 5

Thread: Sticking a period in the middle of a string ;)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    30

    Sticking a period in the middle of a string ;)

    How do I stick a period in the middle of a string?

    I have an integer, for example 123456, and I need to display it as 1234.56 as in currency format. I've tried doing an x.ToString("0.00") but it adds a '.00' at the end (i.e. 123456.00) and x.ToString("#.##") doesn't seem to do anything at all. How is this done? Thanks in advance.

  2. #2
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    Ok I am sure there is an easier way but this should work!

    Code:
    int number = 12345;
    string toFormat = number.ToString();
    string finalnumber = toFormat.Substring(0,toFormat.Length-2);
    finalnumber = finalnumber + "."+ toFormat.Substring(finalnumber.Length-1,toFormat.Length - (finalnumber.Length-1));
    textBox1.Text = finalnumber;
    Hope I could help with that,


    Stephan
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  3. #3
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Code:
    	int val = 123456;
    	string sVal = val.ToString();
    	string formatted = sVal.Insert(sVal.Length - 2, ".");

  4. #4
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    Any of these will do that for you:
    Code:
    x.ToString( n2 );
    x.ToString( N2 );
    x.ToString( f2 );
    x.ToString( F2 );
    The help text for this is hidden under "Standard Numeric Format Strings" in MSDN...

  5. #5
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Talking

    I knew there are easier ways to do it!
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width