Skip to content

Instantly share code, notes, and snippets.

@kuwalab
Created February 16, 2014 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuwalab/9032227 to your computer and use it in GitHub Desktop.
Save kuwalab/9032227 to your computer and use it in GitHub Desktop.
package com.example;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class BigDecimalTest {
public static void main(String... args) {
print(new BigDecimal(1));
print(new BigDecimal(1.12));
print(new BigDecimal("1.12"));
print(new BigDecimal(1.12, new MathContext(1, RoundingMode.FLOOR)));
print(new BigDecimal(0.12, new MathContext(1, RoundingMode.FLOOR)));
BigDecimal bd1 = new BigDecimal(1);
BigDecimal bd3 = new BigDecimal(3);
print(bd1.divide(bd3, new MathContext(3, RoundingMode.FLOOR)));
}
public static void print(BigDecimal bd) {
System.out.println(bd.toPlainString() + " precision=" + bd.precision()
+ " scale=" + bd.scale());
}
}
/* result
1 precision=1 scale=0
1.12000000000000010658141036401502788066864013671875 precision=51 scale=50
1.12 precision=3 scale=2
1 precision=1 scale=0
0.1 precision=1 scale=1
0.333 precision=3 scale=3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment