fork download
  1. import java.math.BigDecimal;
  2.  
  3. class DecimalTest {
  4.  
  5. static int anzahlVorkommastellen(BigDecimal n) {
  6. if (n.intValue() == 0) {
  7. return 1;
  8. }
  9. return n.precision() - n.scale();
  10. }
  11.  
  12. static int anzahlNachkommastellen(BigDecimal n) {
  13. return Math.max(n.stripTrailingZeros().scale(), 0);
  14. }
  15.  
  16. static void check(BigDecimal n) {
  17. System.out.println(n + " : " + anzahlVorkommastellen(n)+" : "+anzahlNachkommastellen(n));
  18. }
  19.  
  20. static public void main(String[] args) {
  21. check(new BigDecimal("999999.99999"));
  22. check(new BigDecimal("999999.999"));
  23. check(new BigDecimal("999999.9"));
  24. check(new BigDecimal("999999"));
  25. check(new BigDecimal("-999999"));
  26. check(new BigDecimal("100"));
  27. check(new BigDecimal("100.000"));
  28. check(new BigDecimal("1234567890"));
  29. check(new BigDecimal("0"));
  30. check(new BigDecimal("0.0000"));
  31. check(new BigDecimal("01.0000"));
  32. check(new BigDecimal("002.0000"));
  33. check(new BigDecimal("0.0001"));
  34. check(new BigDecimal("1.0001"));
  35. check(new BigDecimal("10.0001"));
  36. }
  37. }
  38.  
Success #stdin #stdout 0.17s 58316KB
stdin
Standard input is empty
stdout
999999.99999 : 6 : 5
999999.999 : 6 : 3
999999.9 : 6 : 1
999999 : 6 : 0
-999999 : 6 : 0
100 : 3 : 0
100.000 : 3 : 0
1234567890 : 10 : 0
0 : 1 : 0
0.0000 : 1 : 0
1.0000 : 1 : 0
2.0000 : 1 : 0
0.0001 : 1 : 4
1.0001 : 1 : 4
10.0001 : 2 : 4