我需要四舍五入

编程入门 行业动态 更新时间:2024-10-23 17:38:46
本文介绍了我需要四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨 我需要在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)

更多推荐

我需要四舍五入

本文发布于:2023-10-28 19:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:四舍五入

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!