weightunit.java

来自「Ftp服务1.0」· Java 代码 · 共 49 行

JAVA
49
字号
package ranab.unit;

/**
 * Weight unit converter class.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
class WeightUnit extends BaseUnit {
    
    private final static String[] UNITS = {
        "ounce", 
        "pound", 
        "gram", 
        "kilogram"
    }; 
    
    // base weight unit is kg
    private final static double[] KG_TABLE = {
        35.27396194958041,
        2.2046226218487756,
        1000,
        1    
    };
    
    /**
     * Get all the possible units.
     */
    public String[] getAllUnits() {
        return UNITS;
    }
    
    /**
     * Convert one unit from another
     */
    public String convertUnit(String value, int from, int to) throws UnitException {
        double inVal = getDoubleValue(value);
        double outVal = inVal*KG_TABLE[to]/KG_TABLE[from];
        return String.valueOf(outVal);
    }
    
    /**
     * Return unit type.
     */
    public String toString() {
        return "Weight";
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?