BAEL-2159: Mini Article on "Separate double into integer and decimal parts" (#5300)

This commit is contained in:
Akash Pandey 2018-09-21 15:01:47 +05:30 committed by Grzegorz Piwowarek
parent 1dea2b8cbd
commit 6995a96704
1 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package com.baeldung.doubles;
import java.math.BigDecimal;
public class SplitFloatingPointNumbers {
public static void main(String[] args) {
double doubleNumber = 24.04;
splitUsingFloatingTypes(doubleNumber);
splitUsingString(doubleNumber);
splitUsingBigDecimal(doubleNumber);
}
private static void splitUsingFloatingTypes(double doubleNumber) {
System.out.println("Using Floating Point Arithmetics:");
int intPart = (int) doubleNumber;
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ intPart);
System.out.println("Decimal Part: "+ (doubleNumber - intPart));
}
private static void splitUsingString(double doubleNumber) {
System.out.println("Using String Operations:");
String doubleAsString = String.valueOf(doubleNumber);
int indexOfDecimal = doubleAsString.indexOf(".");
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ doubleAsString.substring(0, indexOfDecimal));
System.out.println("Decimal Part: "+ doubleAsString.substring(indexOfDecimal));
}
private static void splitUsingBigDecimal(double doubleNumber) {
System.out.println("Using BigDecimal Operations:");
BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber));
int intValue = bigDecimal.intValue();
System.out.println("Double Number: "+bigDecimal.toPlainString());
System.out.println("Integer Part: "+intValue);
System.out.println("Decimal Part: "+bigDecimal.subtract(new BigDecimal(intValue)).toPlainString());
}
}