Fix Format

This commit is contained in:
matt.rossi
2019-10-11 18:11:03 +02:00
parent db85c8f275
commit 1076a9c8d9
20374 changed files with 1638947 additions and 0 deletions
@@ -0,0 +1,29 @@
package com.baeldung.maths;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalDemo {
/** Calculate total amount to be paid for an item rounded to cents..
* @param quantity
* @param unitPrice
* @param discountRate
* @param taxRate
* @return
*/
public static BigDecimal calculateTotalAmount(BigDecimal quantity,
BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) {
BigDecimal amount = quantity.multiply(unitPrice);
BigDecimal discount = amount.multiply(discountRate);
BigDecimal discountedAmount = amount.subtract(discount);
BigDecimal tax = discountedAmount.multiply(taxRate);
BigDecimal total = discountedAmount.add(tax);
// round to 2 decimal places using HALF_EVEN
BigDecimal roundedTotal = total.setScale(2, RoundingMode.HALF_EVEN);
return roundedTotal;
}
}
@@ -0,0 +1,51 @@
package com.baeldung.maths;
import java.math.BigDecimal;
public class FloatingPointArithmetic {
public static void main(String[] args) {
double a = 13.22;
double b = 4.88;
double c = 21.45;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
double sum_ab = a + b;
System.out.println("a + b = " + sum_ab);
double abc = a + b + c;
System.out.println("a + b + c = " + abc);
double ab_c = sum_ab + c;
System.out.println("ab + c = " + ab_c);
double sum_ac = a + c;
System.out.println("a + c = " + sum_ac);
double acb = a + c + b;
System.out.println("a + c + b = " + acb);
double ac_b = sum_ac + b;
System.out.println("ac + b = " + ac_b);
double ab = 18.1;
double ac = 34.67;
double sum_ab_c = ab + c;
double sum_ac_b = ac + b;
System.out.println("ab + c = " + sum_ab_c);
System.out.println("ac + b = " + sum_ac_b);
BigDecimal d = new BigDecimal(String.valueOf(a));
BigDecimal e = new BigDecimal(String.valueOf(b));
BigDecimal f = new BigDecimal(String.valueOf(c));
BigDecimal def = d.add(e).add(f);
BigDecimal dfe = d.add(f).add(e);
System.out.println("d + e + f = " + def);
System.out.println("d + f + e = " + dfe);
}
}
@@ -0,0 +1,47 @@
package com.baeldung.maths;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import org.apache.commons.math3.util.Precision;
import org.decimal4j.util.DoubleRounder;
public class Round {
private static final double PI = 3.1415d;
public static void main (String args[]) {
System.out.println("PI: " + PI);
System.out.printf("Value with 3 digits after decimal point %.3f %n", PI);
// OUTPUTS: Value with 3 digits after decimal point 3.142
DecimalFormat df = new DecimalFormat("###.###");
System.out.println(df.format(PI));
System.out.println(round(PI, 3));
System.out.println(roundNotPrecise(PI, 3));
System.out.println(roundAvoid(PI, 3));
System.out.println(Precision.round(PI, 3));
System.out.println(DoubleRounder.round(PI, 3));
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public static double roundNotPrecise(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public static double roundAvoid(double value, int places) {
double scale = Math.pow(10, places);
double rounded = Math.round(value * scale) / scale;
return rounded;
}
}