将英尺和英寸转换为厘米和反之亦然的奇怪问题 [英] Strange issue in converting Feet and Inches to Centimeter and Vice Versa

查看:177
本文介绍了将英尺和英寸转换为厘米和反之亦然的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
}

以上是将Feet和Inches转换为Centimeter.Below的功能。将厘米转换回英尺和英寸。

Above is the function for converting Feet and Inches to Centimeter.Below is the function for converting Centimeter back to Feet and Inches.

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英尺和6英寸,它完美地转换为厘米,并再次转换回5英尺和6英寸。

I have a problem when i enter normal values like 5 Feet and 6 Inches, its converting perfectly to centimeter and again it gets converted back to 5 Feet and 6 Inches.


问题是我转换1英尺和1英寸或2英尺和2美元b $ b英寸,它被转换回1英尺2英寸和2英尺3美元b $ b英寸。

The Problem is when i convert 1 Feet and 1 inches or 2 Feet and 2 inches, its getting converted back to 1 Feet 2 inches and 2 Feet 3 inches.


推荐答案

我相信:

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

应该是:

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

这篇关于将英尺和英寸转换为厘米和反之亦然的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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