GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BAEL-7226: Code for the improvements of the Percentages article. (#15… · edizor/tutorials@9cd13cf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • core-java-modules/core-java-lang-math
    • pom.xml
    • src
      • main/java/com/baeldung/algorithms/percentage
        • BigDecimalPercentageCalculator.java
        • BigDecimalPercentages.java
        • FastBigDecimalPercentage.java
      • test/java/com/baeldung/algorithms/percentage
        • BigDecimalPercentageUnitTest.java
        • FastBigDecimalPercentageUnitTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎core-java-modules/core-java-lang-math/pom.xml‎

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 numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-java-lang-math</artifactId>
77
<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>
816
<packaging>jar</packaging>
917

1018
<parent>
Collapse file

‎core-java-modules/core-java-lang-math/src/main/java/com/baeldung/algorithms/percentage/BigDecimalPercentageCalculator.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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

‎core-java-modules/core-java-lang-math/src/main/java/com/baeldung/algorithms/percentage/BigDecimalPercentages.java‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

‎core-java-modules/core-java-lang-math/src/main/java/com/baeldung/algorithms/percentage/FastBigDecimalPercentage.java‎

Copy file name to clipboard
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

‎core-java-modules/core-java-lang-math/src/test/java/com/baeldung/algorithms/percentage/BigDecimalPercentageUnitTest.java‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

‎core-java-modules/core-java-lang-math/src/test/java/com/baeldung/algorithms/percentage/FastBigDecimalPercentageUnitTest.java‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.