JavaでDecimalFormatを使用して小数点を2桁保持し、0を保持しません.
1240 ワード
import java.text.DecimalFormat;
/**
* Created by laozhu on 2018/4/10.
*/
public class Helo {
public static void main(String[] s){
DecimalFormat decimalFormat = new DecimalFormat("0.##");
double l = 101;
String format = decimalFormat.format(l / 100);
System.out.println(format);
l = 100;
format = decimalFormat.format(l / 100);
System.out.println(format);
l = 110;
format = decimalFormat.format( l / 100);
System.out.println(format);
}
}
上のコードの実行結果は
1.01 1 1.1