C#问题获得3位小数

时间:2017-03-18 13:59:41

标签: c# rounding

我有四舍五入的问题。我划分了用户输入的两个数字,我总是得到' 0' 0或整数。

public static void GetRatios()//This method attains the users ratio inputs
{
    double ratioCalc = waistMeasurment / heightMeasurment; //Calculates the ratio
    ratio = Math.Round((Decimal)ratioCalc, 3, MidpointRounding.AwayFromZero); //Round the answer to 3 decimal places 'ratio' is initialised in the class
}

1 个答案:

答案 0 :(得分:1)

这可能是因为waistMeasurmentheightMeasurmentint

将它们投放到double,您也会在double中获得结果。

你需要将其中一个加入双重才能进行浮点除法(如@doctor在评论中所写):

double ratioCalc = (double)waistMeasurment / heightMeasurment;