4
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 英寸。

4

5 回答 5

3

我运行了以下代码

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。

于 2013-06-12T09:44:29.777 回答
2

我相信:

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

应该:

inchesPart = (int) Math.floor((d / 2.54) - (feetPart * 12));
于 2013-06-12T09:40:47.460 回答
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

于 2013-06-12T09:43:47.287 回答
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.round根据您期望的结果。Math.floorMath.ceil

于 2013-06-12T09:49:38.680 回答
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)
    }
于 2021-04-08T05:34:46.937 回答