我需要四舍五入 [英] I need to round a value
本文介绍了我需要四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我需要在c#中舍入一个值.
示例:
十进制val = 4.1,4.7
我需要的结果是5
删除了喊叫[/编辑]
Hi
I need to round a value in c#.
Example:
decimal val=4.1,4.7
I need the result is 5
Removed shouting[/Edit]
推荐答案
您需要使用
Math.Ceiling(val);
使用数学.Round [ ^ ]或数学天花板 [ Math.Floor [ ^ ]方法.
示例:
Use Math.Round[^] or Math.Ceiling[^] or Math.Floor[^] method.
Example:
Math.Round(4.4); //Returns 4.0.
Math.Round(4.5); //Returns 4.0.
Math.Round(4.6); //Returns 5.0.
您可以通过以下方式进行操作:
You can do it in this way to:
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x; //Result: 1234
有关铸造和类型转换的更多信息 [ int解析之间的主要区别是什么并转换为int32 [
More about Casting and Type Conversions[^].
Please, read it: Whats the main difference between int parse and convert to int32[^]
使用 Math.Round(DecimalValue,MidpointRounding.AwayFromZero)
这篇关于我需要四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文