JScience
Amount.valueOfでparseしてみる
code:java
Amount<Length> k0 = Amount.valueOf(1, SI.KILOMETER);
Amount<Length> k1 = Amount.valueOf(1.0, SI.KILOMETER);
Amount<Length> k2 = (Amount<Length>) Amount.valueOf("1km");
Amount<Length> k3 = (Amount<Length>) Amount.valueOf("1.0km");
Amount<Length> k4 = (Amount<Length>) Amount.valueOf("1 km");
System.out.println("k0 is exact =>" + k0.isExact());
System.out.println("k1 is exact =>" + k1.isExact());
System.out.println("k2 is exact =>" + k2.isExact());
System.out.println("k3 is exact =>" + k3.isExact());
System.out.println("k4 is exact =>" + k4.isExact());
System.out.println("k0==k1 => " + k0.equals(k1));
System.out.println("k0==k2 => " + k1.equals(k2));
System.out.println("k0 approximates k1 => " + k0.approximates(k1));
System.out.println("k0 approximates k2 => " + k1.approximates(k2));
System.out.println("k1 + k2 => " + k1.plus(k2));
k0 is exact =>true
k1 is exact =>false
k2 is exact =>false
k3 is exact =>false
k4 is exact =>true
k0==k1 => false
k0==k2 => true
k0 approximates k1 => true
k0 approximates k2 => true
k1 + k2 => (1.9999999999999998 ± 2.2E-16) km
Amount.valueOf(long val) だと exactになる.Amount.valueOf(double val) だと exactではない.
Amount.valueOf(CharSequence csq) では,スペースでexactになるかどうかが影響される