获取小数位数 [英] Get the number of decimal places

查看:115
本文介绍了获取小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是否有更好的方法来执行此逻辑?我只是想获得小数位数.此逻辑在下面起作用,但是如果输入不是下面的split函数上的小数,我将得到数组超出范围的问题.

Is there better way to do this logic below? I am simply trying to get the number of decimal places. This logic is working below, but I will get an array out of bound issue, if input is not a decimal on the split function below.

//获取小数位数
               b int decimalPlacesNum =           &bb height.ToString(System.Globalization.CultureInfo.InvariantCulture).Split('.')[1] .Length;
               b双倍乘数= Math.Pow(10,decimalPlacesNum);
               b double altConversion =海拔高度*乘数;
               b uint乘数Uint =(uint)乘数;
               b uint heightUint =(uint)altConversion;

        

 //get the number of decimal places
                        int decimalPlacesNum =                      altitude.ToString(System.Globalization.CultureInfo.InvariantCulture).Split('.')[1].Length;
                        double multiplier = Math.Pow(10, decimalPlacesNum);
                        double altConversion = altitude * multiplier;
                        uint multiplierUint = (uint)multiplier;
                        uint altitudeUint = (uint)altConversion;

         

pianoboyCoder

pianoboyCoder

推荐答案

请注意,双精度小数位是一种非常不安全的方法.对于除法(或乘以双倍),将尽最大可能尝试将结果保留在小数点后.

Be aware that the decimal places of a double is a very insecure way. With a divide (or a multiply with doubles) there is made the best attempt of the result behind the decimal point.

如果您真的想使用小数位,而不是使用小数.

If you real want to work with decimal places than use the decimal. 

https://msdn.microsoft.com/en-us/library/364x0z75.aspx


这篇关于获取小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆