JAVA_DecimalFormat digital format

 Original link: https: //blog.csdn.net/icecoola_/article/details/80930377


Class DecimalFormat

import java.text.DecimalFormat;

Double temp = 2.1234;
System.out.println(Double.valueOf(new DecimalFormat("0.00").format(temp)));
System.out.println(Double.valueOf(new DecimalFormat("##0.00").format(temp)));
System.out.println(new DecimalFormat("0.00").format(temp));
System.out.println(new DecimalFormat("#00.00").format(temp));
System.out.println(new DecimalFormat("0.00%").format(temp));

-----------------------------------------------------------------
result:

2.12
2.12
2.12
02.12
212.34% 


demo

double pi = 3.1415927; // pi
// take an integer
System.out.println (new new DecimalFormat ( "0") the format (PI).);. 3 //
// take an integer and two decimal places
System.out .println (new new DecimalFormat ( "0.00") format (PI).); // 3.14
// takes two integers and three decimal places, insufficient to fill the integer part 0.
System.out.println (new new DecimalFormat ( "00.000") the format (PI).); // 03.142
// takes all the integer part
System.out.println (new DecimalFormat ( "#" ) format (pi).); / / 3
// count as a percentage, and taking two decimal places
System.out.println (new DecimalFormat () format ( pi) "# ##%.".); // 314.16%

long c = 299792458; // speed of light
// displayed in scientific notation, and takes five decimal
System.out.println (new DecimalFormat ( "# ##### E0.") format (c).); // 2.99792E8
// display is an integer of two scientific notation, and takes four decimal
System.out.println (new new DecimalFormat ( "E0 00 ####.") the format (C).); // 29.9792E7
/ / every three comma separated.
System.out.println (new DecimalFormat ( ", ###") the format (C).); // 299,792,458
// the embedded text format
System.out.println (new DecimalFormat ( "size of light per second, ## # m ") .format (c));
// size of the speed of light is 299,792,458 meters per second 


Description special character
"0" specified location number does not exist is displayed as 0 123.123 -> 0000.0000 -> 0123.1230
"#" specified location number does not exist is not displayed 123.123 -> #### #### -> 123.123
". "decimal point
"% "will result number is multiplied by 100% behind plus 123.123 -> # 00-> 1.3212%

 

 

Guess you like

Origin www.cnblogs.com/kelelipeng/p/12304586.html