将英尺和英寸转换为厘米和反之亦然的奇怪问题

时间:2013-06-12 09:32:04

标签: java math

public static double convertFeetandInchesToCentimeter(String feet, String inches) {
    double heightInFeet = 0;
    double heightInInches = 0;
    try {
        if (feet != null && feet.trim().length() != 0) {
            heightInFeet = Double.parseDouble(feet);
        }
        if (inches != null && inches.trim().length() != 0) {
            heightInInches = Double.parseDouble(inches);
        }
    } catch (NumberFormatException nfe) {

    }
    return (heightInFeet * 30.48) + (heightInInches * 2.54);
}

上面是将英尺和英寸转换为厘米的功能。下面是将厘米转换回英尺和英寸的功能。

public static String convertCentimeterToHeight(double d) {
    int feetPart = 0;
    int inchesPart = 0;
    if (String.valueOf(d) != null && String.valueOf(d).trim().length() != 0) {
        feetPart = (int) Math.floor((d / 2.54) / 12);
        inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
    }
    return String.format("%d' %d''", feetPart, inchesPart);
}

当我输入像5 Feet and 6 Inches这样的正常值时,我遇到了问题,它完美地转换为厘米,然后又转换回5英尺和6英寸。

  

问题是指我转换1英尺和1英寸或2英尺和2英尺   英寸,它转换回1英尺2英寸和2英尺3   英寸。

5 个答案:

答案 0 :(得分:2)

我相信:

inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));

应该是:

inchesPart = (int) Math.floor((d / 2.54) - (feetPart * 12));

答案 1 :(得分:2)

我运行了以下代码

public class FeetInches{

    public static void main(String[] args){

        double d = convertFeetandInchesToCentimeter("1","1");

        String back_again = convertCentimeterToHeight(d);

        System.out.println(back_again);


    }

    public static double convertFeetandInchesToCentimeter(String feet, String inches) {
        double heightInFeet = 0;
        double heightInInches = 0;
        try {
        if (feet != null && feet.trim().length() != 0) {
            heightInFeet = Double.parseDouble(feet);
        }
        if (inches != null && inches.trim().length() != 0) {
            heightInInches = Double.parseDouble(inches);
        }
        } catch (NumberFormatException nfe) {

        }
        return (heightInFeet * 30.48) + (heightInInches * 2.54);
    }


    public static String convertCentimeterToHeight(double d) {
        int feetPart = 0;
        int inchesPart = 0;
        if (String.valueOf(d) != null && String.valueOf(d).trim().length() != 0) {
        feetPart = (int) Math.floor((d / 2.54) / 12);
        System.out.println((d / 2.54) - (feetPart * 12));
        inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
        }
        return String.format("%d' %d''", feetPart, inchesPart);
    }


}

得到了

1.0000000000000018
1' 2''

通过使用天花板功能,当你真的想要向下舍入到1时,你将四舍五入为。

答案 2 :(得分:1)

问题在于java处理浮点数的方式。

inchesPart = (int) Math.ceil(Math.round((d / 2.54) - (feetPart * 12)));

inchesPart = (int) Math.floor((d / 2.54) - (feetPart * 12));

在输入2,2的情况下,inchesPart的原始值是2.0000000000000036 - > ceil - > 3

答案 3 :(得分:1)

您的代码的主要问题是您没有为每个部分使用相同的舍入功能:

int feetPart = (int) Math.floor((d / 2.54) / 12);
                          ^^^^^
int inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
                            ^^^^

您还应该在分解之前进行舍入以获得一致的结果:

int feetPart = ((int) Math.round(d / 2.54)) / 12;
int inchesPart = ((int) Math.round((d / 2.54)) - (feetPart * 12);

可以将其分解为:

int inches = (int) Math.round(d / 2.54);
int feetPart = inches / 12;
int inchesPart = inches  - (feetPart * 12);

或者( inches - ( ( inches / 12 ) * 12) ) == ( inches % 12 )

int inches = (int) Math.round(d / 2.54);
feetPart = inches / 12;
inchesPart = inches % 12;

您可以将Math.roundMath.floorMath.ceil互换,具体取决于您期望的结果。

答案 4 :(得分:0)

我知道这是旧的可能对其他人有用(Kotlin 版本)

  fun feetToCentimeter(feetval: String): String {
        var heightInFeet = 0.0
        var heightInInches = 0.0
        var feet = ""
        var inches = ""
        if (!TextUtils.isEmpty(feetval)) {
            if (feetval.contains("'")) {
                feet = feetval.substring(0, feetval.indexOf("'"))
            }
            if (feetval.contains("\"")) {
                inches = feetval.substring(feetval.indexOf("'") + 1, feetval.indexOf("\""))
            }
        }
        try {
            if (feet.trim { it <= ' ' }.isNotEmpty()) {
                heightInFeet = feet.toDouble()
            }
            if (inches.trim { it <= ' ' }.isNotEmpty()) {
                heightInInches = inches.toDouble()
            }
        } catch (nfe: NumberFormatException) {
        }

        return (((heightInFeet * 12.0) + heightInInches) * 2.54).toString()
    }

    fun centimeterToFeet(centemeter: String?): String {
        var feetPart = 0
        var inchesPart = 0
        if (!TextUtils.isEmpty(centemeter)) {
            val dCentimeter = java.lang.Double.valueOf(centemeter!!)
            feetPart = Math.floor((dCentimeter / 2.54) / 12).toInt()
            println(dCentimeter / 2.54 - feetPart * 12)
            inchesPart = Math.floor((dCentimeter / 2.54) - (feetPart * 12)).toInt()
        }
        return String.format("%d' %d\"", feetPart, inchesPart)
    }
相关问题