String.Format 格式化没有美分的货币的方法

发布于 2024-09-17 12:48:38 字数 196 浏览 7 评论 0原文

我使用当前方法显示货币

String.Format("{0:C}", item.DonationAmount)

,输出为 10.00 美元。

我们将处理大量美元,而不是美分。我们希望货币显示为 10 美元,不带小数点或零。我该怎么做?删除货币格式会使数字显示为 10.0000 :( 提前致谢。

I'm displaying currency using the current method

String.Format("{0:C}", item.DonationAmount)

Which outputs like $10.00

We will be dealing with large dollar amounts, and no cents. We would like the currency to display as $10 without the decimal or zeroes. How can I do this? Removing the currency format makes the number display like 10.0000 :( thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是我的挚爱i 2024-09-24 12:48:39

指定您想要零小数位:

String.Format("{0:C0}", item.DonationAmount)

Specify you want zero decimal places:

String.Format("{0:C0}", item.DonationAmount)
回忆那么伤 2024-09-24 12:48:39

如果 item.DonationAmount 是一个 non-nullable decimal 那么你可以这样做:

item.DonationAmount.ToString("c0");

如果 item.DonationAmount 是一个您检查的 nullable decimal 有一个值,那么您可以这样做:

item.DonationAmount.Value.ToString("c0");

或者在较新版本的 C# 中,这不需要您检查如果它有一个值:

item.DonationAmount?.ToString("c0");

if item.DonationAmount is a non-nullable decimal then you could just do:

item.DonationAmount.ToString("c0");

if item.DonationAmount is a nullable decimal that you checked has a value then you could do:

item.DonationAmount.Value.ToString("c0");

or in a newer version of C#, which doesn't require you to check if it has a value:

item.DonationAmount?.ToString("c0");
秋凉 2024-09-24 12:48:39
  decimal value = 0.00M;
        value = Convert.ToDecimal(12345.12345);
        Console.WriteLine(".ToString(\"C\") Formates With Currency $ Sign");
        Console.WriteLine(value.ToString("C"));
        //OutPut : $12345.12
        Console.WriteLine(value.ToString("C1"));
        //OutPut : $12345.1
        Console.WriteLine(value.ToString("C2"));
        //OutPut : $12345.12
        Console.WriteLine(value.ToString("C3"));
        //OutPut : $12345.123
        Console.WriteLine(value.ToString("C4"));
        //OutPut : $12345.1235
        Console.WriteLine(value.ToString("C5"));
        //OutPut : $12345.12345
        Console.WriteLine(value.ToString("C6"));
        //OutPut : $12345.123450
        Console.WriteLine();
        Console.WriteLine(".ToString(\"F\") Formates With out Currency Sign");
        Console.WriteLine(value.ToString("F"));
        //OutPut : 12345.12
        Console.WriteLine(value.ToString("F1"));
        //OutPut : 12345.1
        Console.WriteLine(value.ToString("F2"));
        //OutPut : 12345.12
        Console.WriteLine(value.ToString("F3"));
        //OutPut : 12345.123
        Console.WriteLine(value.ToString("F4"));
        //OutPut : 12345.1235
        Console.WriteLine(value.ToString("F5"));
        //OutPut : 12345.12345
        Console.WriteLine(value.ToString("F6"));
        //OutPut : 12345.123450

        Console.WriteLine();
        Console.WriteLine(".ToString(\"N\") Formates With out Currency Sign");
        Console.WriteLine(value.ToString("N"));
        //OutPut : 12,345.12
        Console.WriteLine(value.ToString("N1"));
        //OutPut : 12,345.1
        Console.WriteLine(value.ToString("N2"));
        //OutPut : 12,345.12
        Console.WriteLine(value.ToString("N3"));
        //OutPut : 12,345.123
        Console.WriteLine(value.ToString("N4"));
        //OutPut : 12,345.1235
        Console.WriteLine(value.ToString("N5"));
        //OutPut : 12,345.12345
        Console.WriteLine(value.ToString("N6"));
        //OutPut : 12,345.123450

        Console.WriteLine();
        Console.WriteLine(".ToString(\"P\") Formates With Percentage (i.e multiply by 100)");
        Console.WriteLine(value.ToString("P"));
        //OutPut : 1,2345,12.35%
        Console.WriteLine(value.ToString("P1"));
        //OutPut : 1,2345,12.3%
        Console.WriteLine(value.ToString("P2"));
        //OutPut : 12345,12.35%
        Console.WriteLine(value.ToString("P3"));
        //OutPut : 12345,12.345%
        Console.WriteLine(value.ToString("P4"));
        //OutPut : 12345,12.3450%
        Console.WriteLine(value.ToString("P5"));
        //OutPut : 12345,12.34500%
        Console.WriteLine(value.ToString("P6"));
        //OutPut : 12345,12.345000%


        Console.Read();

单击查看控制台输出屏幕

希望这可以帮助您...

谢谢。 :)

  decimal value = 0.00M;
        value = Convert.ToDecimal(12345.12345);
        Console.WriteLine(".ToString(\"C\") Formates With Currency $ Sign");
        Console.WriteLine(value.ToString("C"));
        //OutPut : $12345.12
        Console.WriteLine(value.ToString("C1"));
        //OutPut : $12345.1
        Console.WriteLine(value.ToString("C2"));
        //OutPut : $12345.12
        Console.WriteLine(value.ToString("C3"));
        //OutPut : $12345.123
        Console.WriteLine(value.ToString("C4"));
        //OutPut : $12345.1235
        Console.WriteLine(value.ToString("C5"));
        //OutPut : $12345.12345
        Console.WriteLine(value.ToString("C6"));
        //OutPut : $12345.123450
        Console.WriteLine();
        Console.WriteLine(".ToString(\"F\") Formates With out Currency Sign");
        Console.WriteLine(value.ToString("F"));
        //OutPut : 12345.12
        Console.WriteLine(value.ToString("F1"));
        //OutPut : 12345.1
        Console.WriteLine(value.ToString("F2"));
        //OutPut : 12345.12
        Console.WriteLine(value.ToString("F3"));
        //OutPut : 12345.123
        Console.WriteLine(value.ToString("F4"));
        //OutPut : 12345.1235
        Console.WriteLine(value.ToString("F5"));
        //OutPut : 12345.12345
        Console.WriteLine(value.ToString("F6"));
        //OutPut : 12345.123450

        Console.WriteLine();
        Console.WriteLine(".ToString(\"N\") Formates With out Currency Sign");
        Console.WriteLine(value.ToString("N"));
        //OutPut : 12,345.12
        Console.WriteLine(value.ToString("N1"));
        //OutPut : 12,345.1
        Console.WriteLine(value.ToString("N2"));
        //OutPut : 12,345.12
        Console.WriteLine(value.ToString("N3"));
        //OutPut : 12,345.123
        Console.WriteLine(value.ToString("N4"));
        //OutPut : 12,345.1235
        Console.WriteLine(value.ToString("N5"));
        //OutPut : 12,345.12345
        Console.WriteLine(value.ToString("N6"));
        //OutPut : 12,345.123450

        Console.WriteLine();
        Console.WriteLine(".ToString(\"P\") Formates With Percentage (i.e multiply by 100)");
        Console.WriteLine(value.ToString("P"));
        //OutPut : 1,2345,12.35%
        Console.WriteLine(value.ToString("P1"));
        //OutPut : 1,2345,12.3%
        Console.WriteLine(value.ToString("P2"));
        //OutPut : 12345,12.35%
        Console.WriteLine(value.ToString("P3"));
        //OutPut : 12345,12.345%
        Console.WriteLine(value.ToString("P4"));
        //OutPut : 12345,12.3450%
        Console.WriteLine(value.ToString("P5"));
        //OutPut : 12345,12.34500%
        Console.WriteLine(value.ToString("P6"));
        //OutPut : 12345,12.345000%


        Console.Read();

Click to see Console Out Put Screen

Hope this may Help you...

Thanks. :)

清风夜微凉 2024-09-24 12:48:39

变化:格式化时不带分,但前提是 cents == 0

使用 c0 将四舍五入到最接近的美元。因此,1514.95 都变为 $15

如果相反,您只想在 $.00 时隐藏分可以使用这个:

value.ToString("c").Replace(".00", "")

给出像 $15$14.95 这样的值

如果有更好的方法请评论!

Variation: formatting without cents, but only if cents == 0

Using c0 will round to the nearest dollar. So 15 and 14.95 both become $15

If instead you wanted to only hide cents if it is $.00 you can use this:

value.ToString("c").Replace(".00", "")

That gives values like $15 and $14.95

If there's a better way please comment!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文