数学取整要求

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

我有一个要求,其中四舍五入为.5及以下的小数,并四舍五入为.6及以上.让我用一个例子来解释你. 13.5-> 13.0是期望值 13.6-> 14.0是期望值 现在我应该编写代码还是在c#中有任何内置功能. 请确认 意思是 13.4-> 13是期望值 13.5-> 13是期望值 13.6-> 14是期望值 请确认我是否必须编码或是否有任何内置功能

Hi, I have a requirement where decimal with .5 and less should be rounded down and decimal with .6 and above should be rounded up. Let me explain you with an example. 13.5 --> 13.0 is the expected value 13.6 --> 14.0 is the expected value Now should i have to code it or is there any built in functionality in c#. Please confirm What is meant is 13.4 --> 13 is the expected value 13.5 --> 13 is the expected value 13.6 --> 14 is the expected value Please confirm if i have to code or is there any in built functionality

推荐答案

double num = 5.5; int intpart = (int)num; double decpart = num - intpart; if (decpart > .5) { intpart = intpart + 1; }

这是你的答案 快乐的编码;)

here is your answer happy coding ;)

Math.Round [ ^ ]

尝试这个逻辑.. Try this logic.. double val = Convert.ToDouble(textBox1.Text); MessageBox.Show(Math.Round((val % .5) == 0 ? (val - 0.1) : val).ToString());

更多推荐

数学取整要求

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

发布评论

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

>www.elefans.com

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