string input = Console.ReadLine();
decimal sum = Convert.ToDecimal(input);
if (sum >= (decimal)500.01)
{
//40% and 8 dollars off shipping costs are taken off total amount
decimal totalprice;
totalprice = (sum - 8) * .60m;
Math.Truncate(totalprice);
Console.WriteLine("Your final cost is:${0:0.00}", totalprice);
Console.Read();
问题是,当我在我的程序中输入价格598.88美元时,我应该得到354.52。截断在C#
数学运算:
598.88 - 8 = 590.88. 590.88 * 60% = 354.528
我真正得到354.53
因为C#,而不是向下取整的了。 例如,
如果我收到类似519.998
的答案,我希望它保持在519.99
。 另一个例子,如果我得到像930.755
这样的答案,我希望它留在930.75
。
我看了一些答案,但Math.Truncate
显然不适用于我,并使用*100/100
技巧也无法正常工作。请记住,我是一名新生,因此,如果答案可能是安全的,那就太好了。谢谢。
'Math.Floor()'.. – zerkms
像zerkms说使用Math.Floor() ,还可以使用Decimal.TryParse来确保输入正确。 – user1534664
'Math.Truncate()'显然不适合你,因为它对它的参数没有任何作用,它返回结果。 – svick