将变量舍入到小数点后两位C# [英] Rounding a variable to two decimal places C#

查看:62
本文介绍了将变量舍入到小数点后两位C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何将变量四舍五入到小数点后两位感兴趣。在下面的示例中,奖金通常是带有四个小数位的数字。有什么方法可以确保薪水变量始终四舍五入到小数点后两位?

I am interested in how to round variables to two decimal places. In the example below, the bonus is usually a number with four decimal places. Is there any way to ensure the pay variable is always rounded to two decimal places?

  pay = 200 + bonus;
  Console.WriteLine(pay);


推荐答案

使用 Math.Round 并指定小数位数。

Use Math.Round and specify the number of decimal places.

Math.Round(pay,2);

Math.Round方法(双精度,Int32)


对a 双精度浮点数值指定为小数位数

Rounds a double-precision floating-point value to a specified number of fractional digits.

Math.Round方法(十进制,Int32)


将十进制值四舍五入为指定的小数位数。

Rounds a decimal value to a specified number of fractional digits.

这篇关于将变量舍入到小数点后两位C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆