您应该使用Decimal数据类型,然后使用一种标准符号,而不是尝试将美分兑换成美元:
Decimal amount = .25M;
String.Format("Amount: {0:C}", amount);
Run Code Online (Sandbox Code Playgroud)
输出是:Amount: $0.25;
class Money
{
public int Dollar {get; set;}
public int Cent { get; set;}
public Money(int cents)
{
this.Dollar = Math.Floor(cents/100);
this.Cent = cents%100;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它
int cents = Convert.ToInt32(Console.Readline("Please enter cents to convert:"))
Money money = new Money(cents);
Console.Writeline("$" + money.Dollar + "." + money.Cent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13739 次 |
| 最近记录: |