none
C# Convert Number to Currency RRS feed

  • Question

  • I am having issue with the converting a text into Currency format. 

    Input: 1000.23

    Expected output: 1,000.23

    I am using the below code, but it is not giving me the right result

    output = string.Format("{0:#,0.#}", float.Parse(input));

    result: output = 1,000.3 ( looks like the float is't working)

    appreciate your help


    Labhesh Shrimali


    Thursday, July 25, 2019 7:55 PM

Answers

  • sorry guys, this worked

    output = string.Format("{0:#,0.##}", float.Parse(input));

    note: additional #


    Labhesh Shrimali

    Thursday, July 25, 2019 7:57 PM
  • An alternative (skipping the parse)

    float value = 1000.23F;
    var result = value.ToString("N2");
    Console.WriteLine(result);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, July 25, 2019 8:08 PM
    Moderator

All replies

  • sorry guys, this worked

    output = string.Format("{0:#,0.##}", float.Parse(input));

    note: additional #


    Labhesh Shrimali

    Thursday, July 25, 2019 7:57 PM
  • An alternative (skipping the parse)

    float value = 1000.23F;
    var result = value.ToString("N2");
    Console.WriteLine(result);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, July 25, 2019 8:08 PM
    Moderator
  • Hi Labhesh,

    Thank you for posting here.

    Based on your description, you want to convert a text into currency format.

    You could try the following code.

                float intput = 1000.23F;
                var output = string.Format("{0:N}", intput);

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 26, 2019 1:53 AM
    Moderator