GeezerVison is showing github.com — BAEL-7226: Code for the improvements of the Percentages article. (#15… · edizor/tutorials@9cd13cf
File treeExpand file treeCollapse file tree Open diff view settings core-java-modules/core-java-lang-math main/java/com/baeldung/algorithms/percentage test/java/com/baeldung/algorithms/percentage
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: core-java-modules/core-java-lang-math/pom.xml +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | <modelVersion>4.0.0</modelVersion> | 6 | 6 | <artifactId>core-java-lang-math</artifactId> | 7 | 7 | <name>core-java-lang-math</name> | | 8 | + <dependencies> | | 9 | + <dependency> | | 10 | + <groupId>org.projectlombok</groupId> | | 11 | + <artifactId>lombok</artifactId> | | 12 | + <version>1.18.24</version> | | 13 | + <scope>compile</scope> | | 14 | + </dependency> | | 15 | + </dependencies> | 8 | 16 | <packaging>jar</packaging> | 9 | 17 |
| 10 | 18 | <parent> | |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.algorithms.percentage; | | 2 | + | | 3 | +import lombok.experimental.ExtensionMethod; | | 4 | +import java.math.BigDecimal; | | 5 | +import java.util.Scanner; | | 6 | + | | 7 | +@ExtensionMethod(FastBigDecimalPercentage.class) | | 8 | +public class BigDecimalPercentageCalculator { | | 9 | + public static void main(String[] args) { | | 10 | + | | 11 | + Scanner in = new Scanner(System.in); | | 12 | + System.out.println("Enter obtained marks:"); | | 13 | + BigDecimal obtained = new BigDecimal(in.nextDouble()); | | 14 | + System.out.println("Enter total marks:"); | | 15 | + BigDecimal total = new BigDecimal(in.nextDouble()); | | 16 | + | | 17 | + System.out.println("Percentage obtained :"+ obtained.toPercentageOf(total)); | | 18 | + } | | 19 | + | | 20 | +} |
Collapse file Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.algorithms.percentage; | | 2 | + | | 3 | + | | 4 | +import java.math.BigDecimal; | | 5 | +import java.math.RoundingMode; | | 6 | + | | 7 | +public class BigDecimalPercentages { | | 8 | + | | 9 | + private static final BigDecimal ONE_HUNDRED = new BigDecimal("100"); | | 10 | + | | 11 | + public BigDecimal toPercentageOf(BigDecimal value, BigDecimal total) { | | 12 | + return value.divide(total, 4, RoundingMode.HALF_UP).multiply(ONE_HUNDRED); | | 13 | + } | | 14 | + | | 15 | + public BigDecimal percentOf(BigDecimal percentage, BigDecimal total) { | | 16 | + return percentage.multiply(total).divide(ONE_HUNDRED, 2, RoundingMode.HALF_UP); | | 17 | + } | | 18 | +} |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.algorithms.percentage; | | 2 | + | | 3 | +import java.math.BigDecimal; | | 4 | +import java.math.RoundingMode; | | 5 | + | | 6 | +public class FastBigDecimalPercentage { | | 7 | + | | 8 | + public static BigDecimal toPercentageOf(BigDecimal value, BigDecimal total) { | | 9 | + return value.divide(total, 4, RoundingMode.HALF_UP).scaleByPowerOfTen(2); | | 10 | + } | | 11 | + | | 12 | + public static BigDecimal percentOf(BigDecimal percentage, BigDecimal total) { | | 13 | + return percentage.multiply(total).scaleByPowerOfTen(-2); | | 14 | + } | | 15 | + | | 16 | +} |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.algorithms.percentage; | | 2 | + | | 3 | +import org.hamcrest.number.BigDecimalCloseTo; | | 4 | +import org.junit.Assert; | | 5 | +import org.junit.Test; | | 6 | + | | 7 | +import java.math.BigDecimal; | | 8 | + | | 9 | +public class BigDecimalPercentageUnitTest { | | 10 | + private BigDecimalPercentages pc = new BigDecimalPercentages(); | | 11 | + | | 12 | + | | 13 | + @Test | | 14 | + public void shouldConvertToPercentageOfTotal(){ | | 15 | + BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(5.05), new BigDecimal(0.001)); | | 16 | + Assert.assertTrue("Result not as expected",expected.matchesSafely( | | 17 | + pc.toPercentageOf(new BigDecimal(50.5),new BigDecimal(1000)))); | | 18 | + } | | 19 | + | | 20 | + @Test | | 21 | + public void shouldCalculatePercentageOfTotal(){ | | 22 | + BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(31.40), new BigDecimal(0.001)); | | 23 | + Assert.assertTrue("Result not as expected",expected.matchesSafely( | | 24 | + pc.percentOf(new BigDecimal(3.14),new BigDecimal(1000)))); | | 25 | + } | | 26 | + | | 27 | +} |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.algorithms.percentage; | | 2 | + | | 3 | +import org.hamcrest.number.BigDecimalCloseTo; | | 4 | +import org.junit.Assert; | | 5 | +import org.junit.Test; | | 6 | + | | 7 | +import java.math.BigDecimal; | | 8 | + | | 9 | +public class FastBigDecimalPercentageUnitTest { | | 10 | + private FastBigDecimalPercentage pc = new FastBigDecimalPercentage(); | | 11 | + | | 12 | + | | 13 | + @Test | | 14 | + public void shouldConvertToPercentageOfTotal(){ | | 15 | + BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(5.05), new BigDecimal(0.001)); | | 16 | + Assert.assertTrue("Result not as expected",expected.matchesSafely( | | 17 | + pc.toPercentageOf(new BigDecimal(50.5),new BigDecimal(1000)))); | | 18 | + } | | 19 | + | | 20 | + @Test | | 21 | + public void shouldCalculatePercentageOfTotal(){ | | 22 | + BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(31.40), new BigDecimal(0.001)); | | 23 | + Assert.assertTrue("Result not as expected",expected.matchesSafely( | | 24 | + pc.percentOf(new BigDecimal(3.14),new BigDecimal(1000)))); | | 25 | + } | | 26 | + | | 27 | +} |
|