Basic usage of [android] DecimalFormat

DecimalFormat is very powerful. Here are just some basic usage

(0) truncate integer part

DecimalFormat df = new DecimalFormat("0");
df.setRoundingMode(RoundingMode.HALF_DOWN);
log(df.format(123.123));//Results: 123
log(df.format(123.0));//Results: 123
log(df.format(0.123));//Results: 0

(0.) truncate the whole part and one decimal place

DecimalFormat df = new DecimalFormat("0.0");
df.setRoundingMode(RoundingMode.HALF_DOWN);
log(df.format(123.123));//Results: 123.1
log(df.format(123.0));//Results: 123
log(df.format(0.123));//Results: 0.1

(0.) truncate the integer part (fill 0 if there is no first two digits) and two digits after the decimal point

DecimalFormat df = new DecimalFormat("00.00");
df.setRoundingMode(RoundingMode.HALF_DOWN);
log(df.format(123.123));//Results: 123.12
log(df.format(123.0));//Results: 123.00
log(df.format(0.123));//Result: 00.12

(x) truncate the integer part

DecimalFormat df = new DecimalFormat("#");
df.setRoundingMode(RoundingMode.HALF_DOWN);
log(df.format(123.123));//Results: 123
log(df.format(123.0));//Results: 123
log(df.format(0.123));//Results: 0

(ා) truncate the integer part and one digit after the decimal point (when the decimal part is 0, no decimal will be displayed)

DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.HALF_DOWN);
log(df.format(123.123));//Results: 123.1
log(df.format(123.0));//Results: 123
log(df.format(0.123));//Results: 0.1

(ා) truncate the integer part and two digits after the decimal point (when the decimal part is 0, no decimal will be displayed)

DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.HALF_DOWN);
log(df.format(123.123));//Results: 123.12
log(df.format(123.0));//Results: 123
log(df.format(0.123));//Result: 0.12

(, ×) every 3 bits of integer part are separated by

DecimalFormat df = new DecimalFormat(",###");
log(df.format(123123123.123));//Results: 123123123

(, ×. 0) used for every three digits of integer part, separated by two digits after the decimal (when the decimal part is 0, display. 00)

DecimalFormat df = new DecimalFormat(",###.00");
log(df.format(123123123.123));//Results: 123123123.12
log(df.format(123.0));//Results: 123.00
log(df.format(0.123));//Results:.12

(×. ×%) is displayed as a percentage, and one decimal place is reserved

DecimalFormat df = new DecimalFormat("#.#%");
log(df.format(123.123));//Results: 12312.3%
log(df.format(123.0));//Result: 12300%
log(df.format(0.123));//Results: 12.3%

(- ×) display as negative

DecimalFormat df = new DecimalFormat("-#");
log(df.format(123123123.123));//Results: - 123123123
log(df.format(123.0));//Results: - 123
log(df.format(0.123));//Results: -0

(ා. Character) truncates the integer part, retains one decimal place, and ends with the string kg

DecimalFormat df = new DecimalFormat("#.# kg");
log(df.format(123.123));//Results: 123.1 kg

(x character) selects the integer part, ending with the string kilogram

DecimalFormat df = new DecimalFormat("# Kilogram ";
log(df.format(123123123.123));//Results: 123123kg
log(df.format(123.0));//Result: 123 kg
log(df.format(0.123));//Result: 0 kg

Round to one decimal place

DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.HALF_UP);
log(df.format(123.15));//Results: 123.2
log(df.format(123.14));//Results: 123.1

 

Added by lasse48 on Wed, 01 Jan 2020 05:21:21 +0200