在 c# 中已使用 ToString 作为小数时添加千位分隔符

问题描述 投票:0回答:1

我正在使用实体框架在 .net 7 上执行我的程序。

我需要像这样设置价格格式:1'234.56 然而,它们看起来像这样:1234.56

f.Price.ToString("0.00");

问题是,如您所见,我已经使用 ToString 函数将小数点后四舍五入为 2。

我知道我可以使用 .ToString("N");格式化它,但我不知道如何在一个 ToString 中使用两者,我的尝试都因问题而结束,并且应用程序不再启动。

有谁知道如何使用千位分隔符+仅 2 位小数来格式化我的价格?

c# .net entity-framework formatting tostring
1个回答
1
投票

如果您使用 .NET 7,您可以使用

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

参考工作代码这里

© www.soinside.com 2019 - 2024. All rights reserved.