Java/Data Type/Decimal
Содержание
- 1 Change the decimal separator is set to "."
- 2 Convert a number using the current Locale()
- 3 DecimalFormat("0000000000E0")
- 4 DecimalFormat("00.00E0")
- 5 DecimalFormat("000E00")
- 6 DecimalFormat("00E00")
- 7 DecimalFormat("##E0") (exponent must be multiple of 2)
- 8 DecimalFormat("###E0") (exponent must be multiple of 3)
- 9 Display a currency value
- 10 Display a percentage
- 11 Display numbers with commas
- 12 Display numbers with leading zeroes
- 13 Display the amount of free memory in the Java Virtual Machine.
- 14 Display the maximum amount of memory
- 15 Display the total amount of memory in the Java virtual machine.
- 16 Force minimum number of digits to left and right of decimal point
- 17 new DecimalFormat("#.#")
- 18 new DecimalFormat("##00")
- 19 new DecimalFormat("0.00")
- 20 new DecimalFormat("#.000000")
- 21 new DecimalFormat("000000E0")
- 22 new DecimalFormat("0.######E0")
- 23 new DecimalFormat("#.######") (1)
- 24 new DecimalFormat(".######") (2)
- 25 new DecimalFormat(abc#)
- 26 new DecimalFormat("#,###,###") (grouping)
- 27 Round number to fewer decimals
- 28 Set format to two decimal places: set Maximum Fraction Digits
- 29 Set format to two decimal places: set Minimum Fraction Digits
- 30 The 0 symbol shows a digit or 0 if no digit present
- 31 The number of #"s to the left of the decimal point sets the multiple of the exponent.
- 32 The . symbol indicates the decimal point
- 33 The , symbol is used to group numbers
- 34 The " symbol is used to quote literal symbols
- 35 The ; symbol is used to specify an alternate pattern for negative values
- 36 Use group separators and show trailing zeroes
- 37 Use java.text.DecimalFormat to format integer
- 38 Use new DecimalFormat("0.#####E0") to format double
Change the decimal separator is set to "."
<source lang="java">
import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; public class Main {
public static void main(String args[]) { double d = 123456.7890; DecimalFormat df = new DecimalFormat("#####0.00"); DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); dfs.setDecimalSeparator("."); df.setDecimalFormatSymbols(dfs); System.out.println(df.format(d)); }
}
</source>
Convert a number using the current Locale()
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] args) throws Exception { System.out.println(NumberFormat.getInstance(new Locale("us")).format(12345.12345)); }
} //12,345.123
</source>
DecimalFormat("0000000000E0")
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("0000000000E0"); String s = formatter.format(-1234.567); // -1234567000E-6 System.out.println(s); }
}
</source>
DecimalFormat("00.00E0")
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("00.00E0"); String s = formatter.format(-1234.567); // -12.35E2 System.out.println(s); s = formatter.format(-.1234567); // -12.35E-2 System.out.println(s); }
}
</source>
DecimalFormat("000E00")
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("000E00"); String s = formatter.format(-1234.567); // -123E01 System.out.println(s); }
}
</source>
DecimalFormat("00E00")
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("00E00"); String s = formatter.format(-1234.567); System.out.println(s); }
} // -12E02
</source>
DecimalFormat("##E0") (exponent must be multiple of 2)
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("##E0"); String s = formatter.format(-1234.567); System.out.println(s); s = formatter.format(-123.4567); System.out.println(s); s = formatter.format(-12.34567); System.out.println(s); }
} // -12E2 // -1.2E2 // -12E0
</source>
DecimalFormat("###E0") (exponent must be multiple of 3)
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) { DecimalFormat formatter = new DecimalFormat("###E0"); String s = formatter.format(-1234.567); // -1.23E3 System.out.println(s); s = formatter.format(-123.4567); // -123E0 System.out.println(s); s = formatter.format(-12.34567); // -12.3E0 System.out.println(s); s = formatter.format(-1.234567); // -12.3E0 System.out.println(s); s = formatter.format(-.1234567); // -123E-3 System.out.println(s); }
}
</source>
Display a currency value
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat df = new DecimalFormat("\u00a4#,##0.00"); System.out.println(df.format(4232.19)); System.out.println(df.format(-4232.19)); }
}
</source>
Display a percentage
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat df = new DecimalFormat("#%"); System.out.println(df.format(0.19)); System.out.println(df.format(-0.19)); }
} /*19% -19%
- /
</source>
Display numbers with commas
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String args[]) { double d = 123456.7890; DecimalFormat df = new DecimalFormat("#####0.00"); System.out.println(df.format(d)); }
} //123456.79
</source>
Display numbers with leading zeroes
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String args[]) { DecimalFormat df = new DecimalFormat("0000000000000000"); String z = df.format(123456); System.out.println(z); }
}
</source>
Display the amount of free memory in the Java Virtual Machine.
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] args) { DecimalFormat df = new DecimalFormat("0.00"); long freeMem = Runtime.getRuntime().freeMemory(); System.out.println(df.format(freeMem / 1000000F) + " MB"); }
}
</source>
Display the maximum amount of memory
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] args) { DecimalFormat df = new DecimalFormat("0.00"); long maxMem = Runtime.getRuntime().maxMemory(); System.out.println(df.format(maxMem / 1000000F) + " MB"); }
}
</source>
Display the total amount of memory in the Java virtual machine.
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] args) { DecimalFormat df = new DecimalFormat("0.00"); long totalMem = Runtime.getRuntime().totalMemory(); System.out.println(df.format(totalMem / 1000000F) + " MB"); }
}
</source>
Force minimum number of digits to left and right of decimal point
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("0.0E0"); String s = formatter.format(-1234.567); // -1.2E3 System.out.println(s); }
}
</source>
new DecimalFormat("#.#")
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("#.#"); String s = formatter.format(-1234.567); // -1234.6 System.out.println(s); }
}
</source>
new DecimalFormat("##00")
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("##00"); String s = formatter.format(0); // 00 System.out.println(s); }
}
</source>
new DecimalFormat("0.00")
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("0.00"); String s = formatter.format(-.567); // -0.57 System.out.println(s); }
}
</source>
new DecimalFormat("#.000000")
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("#.000000"); String s = formatter.format(-1234.567); System.out.println(s); }
} //-1234.567000
</source>
new DecimalFormat("000000E0")
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String args[]) { System.out.println(new DecimalFormat("000000E0").format(123123)); }
} //123123E0
</source>
new DecimalFormat("0.######E0")
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String args[]) { System.out.println(new DecimalFormat("0.######E0").format(12345)); }
} //1.2345E4
</source>
new DecimalFormat("#.######") (1)
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("#.######"); String s = formatter.format(-1234.567); // -1234.567 System.out.println(s); }
}
</source>
new DecimalFormat(".######") (2)
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat(".######"); String s = formatter.format(-1234.567); // -1234.567 System.out.println(s); }
}//-1234.567
</source>
new DecimalFormat(abc#)
<source lang="java">
import java.text.DecimalFormat; import java.text.Format; public class Main {
public static void main(String[] argv) throws Exception { Format formatter = new DecimalFormat(""abc"#"); String s = formatter.format(-1234.567); System.out.println(s); }
} // -abc1235
</source>
new DecimalFormat("#,###,###") (grouping)
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("#,###,###"); String s = formatter.format(-1234.567); System.out.println(s); s = formatter.format(-1234567.890); System.out.println(s); }
} // -1,235 // -1,234,568
</source>
Round number to fewer decimals
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] args) { double numberToRound = 12345.6789; DecimalFormat df = new DecimalFormat("0.000"); System.out.println("Rounded number = " + df.format(numberToRound)); System.out.println(String.format("Rounded number = %.3f", numberToRound)); }
} /* Rounded number = 12345.679 Rounded number = 12345.679
- /
</source>
Set format to two decimal places: set Maximum Fraction Digits
<source lang="java">
import java.text.NumberFormat; public class Main {
public static void main(String args[]) { NumberFormat nf = NumberFormat.getInstance(); System.out.println("Default format: " + nf.format(1234567.678)); nf.setMaximumFractionDigits(2); System.out.println("Format with two decimal places: " + nf.format(1234567.678)); }
} /* Default format: 1,234,567.678 Format with two decimal places: 1,234,567.68
- /
</source>
Set format to two decimal places: set Minimum Fraction Digits
<source lang="java">
import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); System.out.println("Format with two decimal places: " + nf.format(1234567.678)); }
}
</source>
The 0 symbol shows a digit or 0 if no digit present
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("000000"); String s = formatter.format(-1234.567); System.out.println(s); // number was rounded up }
} //-001235
</source>
The number of #"s to the left of the decimal point sets the multiple of the exponent.
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("#E0"); // exponent can be any // value String s = formatter.format(-1234.567); System.out.println(s); s = formatter.format(-.1234567); System.out.println(s); }
} //-.1E4 //-.1E0
</source>
The . symbol indicates the decimal point
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat(".00"); String s = formatter.format(-.567); // -.57 System.out.println(s); }
}
</source>
The , symbol is used to group numbers
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("#,###,###"); String s = formatter.format(-1234.567); System.out.println(s); s = formatter.format(-1234567.890); System.out.println(s); }
} // -1,235 // -1,234,568
</source>
The " symbol is used to quote literal symbols
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat(""#"#"); String s = formatter.format(-1234.567); System.out.println(s); }
} //-#1235
</source>
The ; symbol is used to specify an alternate pattern for negative values
<source lang="java">
public class Main{ public static void main(String[] argv) throws Exception{
NumberFormat formatter = new DecimalFormat("#;(#)"); String s = formatter.format(-1234.567); // (1235) sysout
}}
</source>
Use group separators and show trailing zeroes
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String[] argv) throws Exception { DecimalFormat df = new DecimalFormat("#,.00;(#,.00)"); System.out.println(df.format(7123.00)); }
}
</source>
Use java.text.DecimalFormat to format integer
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String args[]) { System.out.println(new DecimalFormat("0.#####E0").format(123456)); }
} //1.23456E5
</source>
Use new DecimalFormat("0.#####E0") to format double
<source lang="java">
import java.text.DecimalFormat; public class Main {
public static void main(String args[]) { double d = 0.12345; System.out.println(new DecimalFormat("0.#####E0").format(d)); }
} //1.2345E-1
</source>