C#浮点数总是向上

我想在 C# 中实现一个函数,它需要一个浮点数,它会输出四舍五入的一个(总是向上):


1,527 -> 1,53

1,53 -> 1,6

1,6 -> 2

最简单的方法是什么?


编辑:稍微澄清一下 - 我希望它设置适当的 2D 图形轴的最大值,所以如果最大值是 1,527,我想做这个函数的几次迭代,直到舍入值高于例如 10%,所以对于 1,527,最大值可能是 1,6,因此 1,527 适合几乎同时完全使用的图


湖上湖
浏览 200回答 4
4回答

qq_遁去的一_1

使用这个问题的标记答案:static void Main(){&nbsp; &nbsp; Console.WriteLine("DECIMAL");&nbsp; &nbsp; decimal dTest = 1.527m;&nbsp; &nbsp; var dTest2 = dTest;&nbsp; &nbsp; while(dTest2 < dTest*1.1m)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; dTest2 = RoundUp(dTest2);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(dTest2);&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine("FLOAT");&nbsp; &nbsp; float fTest = 1.527f;&nbsp; &nbsp; var fTest2 = fTest;&nbsp; &nbsp; while(fTest2 < fTest*1.1f)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; fTest2 = RoundUp(fTest2);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(fTest2);&nbsp; &nbsp; }}static decimal RoundUp(decimal input){&nbsp; &nbsp; int precision = BitConverter.GetBytes(decimal.GetBits(input)[3])[2];&nbsp; &nbsp; decimal factor = (decimal)Math.Pow(10,precision-1);&nbsp; &nbsp; return Math.Ceiling(input*factor)/factor;}static float RoundUp(float input){&nbsp; &nbsp; return (float)RoundUp((decimal)input);}输出:DECIMAL1.531.62FLOAT1.531.62

泛舟湖上清波郎朗

为什么不通过简单的 10 次幂乘法来实现,这可以有效地移动小数点,您可以在其中调用 Math.Ceiling 将数字向上取整。除以 10 的相同幂以将小数点放回原来的位置。使用decimal.ToString()为“避开”的浮点精度的问题,看到这个博客帖子获取更多信息var values = new[] { 1.527f, 1.53f, 1.6f, -1.527f };for (var i = 0; i < values.Length; i++){&nbsp; &nbsp; var val = (decimal)values[i];&nbsp; &nbsp; var precision = (decimal.GetBits(val)[3] >> 16 & 0xff) - 1;&nbsp; &nbsp; var power = (decimal)Math.Pow(10, precision);&nbsp; &nbsp; if (val < 0)&nbsp; &nbsp; &nbsp; &nbsp; val = Math.Floor(val * power) / power;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; val = Math.Ceiling(val * power) / power;&nbsp; &nbsp; Console.WriteLine(val);}输出1.531.62-1.53注意 Math.Pow和Math.Ceiling是对 的操作double,因此强制转换为doublefromfloat更新计算出它需要舍入到多少小数位并使用小数来“解决”浮点精度问题。更新 2用于decimal.GetBytes获得数字的精度,而不是执行相当繁琐的文化不变ToString().Splityada yada yada。更新 3对于负数,从零开始舍入并按位添加& 0xff以去除符号位。

白猪掌柜的

独立于文化的解决方案:static double RoundUp(double val){&nbsp; &nbsp; double a = val;&nbsp; &nbsp; double decimals = a - ((int)a); //Gets only decimals&nbsp; &nbsp; double pow = Math.Pow(10, decimals.ToString().Length - 3);&nbsp;&nbsp; &nbsp; a = a * pow;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Multiply by a power of 10 | decimal shift&nbsp; &nbsp; a = Math.Ceiling(a);&nbsp; &nbsp; &nbsp;//Round up&nbsp; &nbsp; a = a / pow;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Shift back&nbsp; &nbsp; return a;}并称之为:var res = RoundUp(1.527); //1.53res = RoundUp(1.53); //1.6res = RoundUp(1.6); //2或对于浮点数:static float RoundUp(float val){&nbsp; &nbsp; float a = val;&nbsp; &nbsp; float t = a - ((int)a); //Gets only decimals&nbsp; &nbsp; float pow = (float)Math.Pow(10, t.ToString().Length - 3);&nbsp;&nbsp; &nbsp; a = a * pow;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Multiply by a power of 10 | decimal shift&nbsp; &nbsp; a = (float)Math.Ceiling(a);&nbsp; &nbsp; &nbsp;//Round up&nbsp; &nbsp; a = a / pow;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Shift back&nbsp; &nbsp; return a;}var res = RoundUp(1.527f); //1.53res = RoundUp(1.53f); //1.6res = RoundUp(1.6f); //2

不负相思意

var&nbsp;a&nbsp;=&nbsp;"1,53";将此字符串拆分为两个,并计算小数位数:var&nbsp;length&nbsp;=&nbsp;a.Split(',')[1].Length;将原始字符串转换为双变量(替换,为.以避免转换过程中出现异常):var&nbsp;b&nbsp;=&nbsp;Convert.ToDouble(a.Replace(',',&nbsp;'.'))以指定的精度执行舍入:var&nbsp;c&nbsp;=&nbsp;Math.Ceil(b,&nbsp;(length&nbsp;-&nbsp;1));并返回值替换.为,:return&nbsp;c.ToString().Replace('.',&nbsp;',');
打开App,查看更多内容
随时随地看视频慕课网APP