Add Thousand Separator when already using ToString for decimals in c#

58 Views Asked by At

I'm doing my program on .net 7 using entity framework.

I need to format my prices like this : 1'234.56 However, they're appearing like this : 1234.56

f.Price.ToString("0.00");

The problem is that as you can see, i'm already using ToString function to round decimals to 2 after the dot.

I know I could use .ToString("N"); to format it, but i don't know how I could use both in a single ToString, my attempts were all ending by problems and the application not starting anymore.

Does anyone knows how I could format my prices with a thousand separator + only 2 decimals ?

1

There are 1 best solutions below

0
Selaka Nanayakkara On BEST ANSWER

If you are using .NET 7 you can use ToString("N2")

using System;

class Program
{
    static void Main()
    {
        var price = 12348888.56;     
        string formattedPrice = price.ToString("N2"); // Use "N" format specifier to add a thousand separator
        Console.WriteLine(result);
    }
}

Refer the working code here