Created
February 16, 2014 10:32
-
-
Save kuwalab/9032227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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