[英]Rounding decimals to two decimal places in c#
I want to round 54.5345 to 54.54 that is if I have a third decimal place then i want to add 1 to the 2nd decimal place. 我想将54.5345舍入到54.54,如果我有第三个小数位,那么我想将1加到第二个小数位。
I have tried using math.round but it always rounds down if the third decimal is less than 5 我尝试过使用math.round,但如果第三个小数小于5,它总是向下舍入
try: 尝试:
d = Math.Ceiling(d * 100) / 100;
where d is your decimal. 其中d是你的小数。
I think you should try this: 我想你应该试试这个:
double a = Math.Round(-57.5345, 2);
This works for negative numbers too. 这也适用于负数。
The way you are rounding off is not correct. 四舍五入的方式不正确。
You can also refer: 你也可以参考:
How do you round a number to two decimal places in C#? 你如何在C#中将数字四舍五入到小数点后两位?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.