使用 DecimalFormat 对运算结果按照指定格式输出。
具体代码如下所示:
package com.data;
import java.text.DecimalFormat;
public class TestLong01 {
public static void main(String[] args) {
Long num1 = 5L;
Long num2 = 3L;
DecimalFormat df = new DecimalFormat("0.00");
double result = (num1.longValue() - num2.longValue())*1.0 / num2.longValue();
double result2 = (num1.longValue() - num2.longValue()) / num2.longValue();
System.out.println("result为:" + result);
System.out.println("result2为:" + result2);
System.out.println("格式化result为:" + df.format(result * 100) + "%");
}
}
运行结果:
参考文章: JAVA的除法计算