String.Format方法来格式货币没有分
我使用当前的方法显示货币
String.Format("{0:C}", item.DonationAmount)
哪个输出像$ 10.00
我们将处理大量美元,而不是美分。 我们希望货币显示为10美元,不含小数或零。 我怎样才能做到这一点? 删除货币格式使数字显示像10.0000 :(提前致谢。
指定你想要零位小数:
String.Format("{0:C0}", item.DonationAmount)
如果item.DonationAmount
是一个decimal
那么你可以这样做:
item.DonationAmount.ToString("c0");
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 (ie 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();
点击查看控制台输出屏幕
希望这可以帮助你…
谢谢。 🙂