Question

I'm trying to convert a number so it looks like the formatting in money. I need to take 258000 and make it 2,580.00 or 25000 and make it 250.00 or 360 and make it 3.60 This is what I'm using but it's adding the ".00" at the end of all numbers making 2500 2500.00 but it should be 25.00.

Value = string.Format("{0:##,###.00}", Convert.ToDecimal(Value));
Was it helpful?

Solution

It seems to me that you're just missing the fact that you can divide the user's input by 100 after parsing it:

using System;

class Program
{
    static void Main(string[] args)
    {
        string input = "2500";
        decimal cents = decimal.Parse(input); // Potentially use TryParse...
        decimal dollars = cents / 100m;
        string output = dollars.ToString("0.00");
        Console.WriteLine(output); // 25.00
    }
}

Note that there are complicated cultural rules around how currency values should be displayed - I would suggest using the C format specifier, using a CultureInfo which is like whatever the users are expecting, but with the NumberFormatInfo.CurrencySymbol set to an empty string.

You should also consider which culture to use when parsing the user's input - it can significantly affect the results if they decide to use grouping separators or decimal separators. Will they always be entering an integer? If so, parse it as an integer too.

OTHER TIPS

double valueOriginal = 260;
Response.Write( (valueOriginal / 100).ToString("C"));

260 = (206/100) then (260/100).ToString("C");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top