Convert double type to string

public static void main(String[] args) {
double priceWithFreight = 1200.5698d;
System.out.println(priceWithFreight);

//The bug that double to string will turn into a comma when the decimal point exceeds 1000, do not use this method
NumberFormat nf = NumberFormat. getInstance();
nf.setRoundingMode(RoundingMode.HALF_UP);//Set the rounding
nf.setMaximumFractionDigits(2);//Set the maximum number of decimal places
String str= nf.format(priceWithFreight);
System.out.println(str) ; // output: 1,200.57 

//correct double to string method
BigDecimal b = new BigDecimal(priceWithFreight);
String str1=String.valueOf(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
System.out .println(str1); //output: 1200.57
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324888377&siteId=291194637