本文介绍了我需要四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨 我需要在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)
更多推荐
我需要四舍五入
发布评论