Skip to content

Commit

Permalink
Corrected decimal alignment display bug
Browse files Browse the repository at this point in the history
- Adding number without decimal would not align
- Added a test for the new queryDecimalIndex() method
- Updated pom.xml to ignore windows error on `chmod` for
  exec-maven-plugin
- Updated maven exec plugin from 3.3.0 -> 3.4.0
- Updated JUnit from 5.11.0-M2 -> 5.11.0-RC1
  • Loading branch information
frossm committed Aug 7, 2024
1 parent ee67656 commit 4139a9d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
13 changes: 8 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>rpncalc</artifactId>
<version>5.2.9</version>
<version>5.2.10</version>
<packaging>jar</packaging>

<name>rpncalc</name>
Expand Down Expand Up @@ -208,7 +208,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
<executions>
<execution>
<id>chmod</id>
Expand All @@ -220,9 +220,12 @@
<executable>chmod</executable>
<arguments>
<argument>+x</argument>
<argument>
${project.build.directory}/${project.name}.jar</argument>
<argument>${project.build.directory}/${project.name}.jar</argument>
</arguments>
<successCodes>
<successCode>0</successCode>
<successCode>1</successCode>
</successCodes>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -273,7 +276,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.0-M2</version>
<version>5.11.0-RC1</version>
<scope>test</scope>
</dependency>

Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: rpncalc
version: '5.2.9'
version: '5.2.10'
summary: The command line Reverse Polish Notation (RPN) calculator
description: |
RPNCalc is an easy to use command line based Reverse Polish
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/fross/rpncalc/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ public class Display {
*/
@SuppressWarnings("MalformedFormatString") // No idea what's wrong with the return statement
public static String Comma(BigDecimal bd) {
Output.debugPrintln("BigDecimal Scale: " + bd.scale());
// Output.debugPrintln("Comma: BigDecimal Scale: " + bd.scale());
return String.format("%,." + bd.scale() + "f", bd);
}

/**
* queryDecimalIndex(): Return the index of the decimal in a string. If none is given assume it's at the end.
*
* @param str - String with or without a decimal point in it
* @return An integer with the location of the decimal (or the end if it doesn't exist)
*/
public static int queryDecimalIndex(String str) {
// Determine where the decimal point is located. If no decimal exists (-1) assume it's at the end
int di = str.indexOf(".");
if (di == -1) {
di = str.length();
}
return di;
}

/**
* DisplayStatusLine(): Display the last line of the header and the separator line. This is a separate function given it also
* inserts the loaded stack and spaces everything correctly.
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/fross/rpncalc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public static void main(String[] args) {
currentStackItem = Display.Comma(calcStack.get(i));
}

// Determine where the decimal point is located
decimalIndex = currentStackItem.indexOf(".");
// Determine where the decimal point is located. If no decimal exists (-1) assume it's at the end
decimalIndex = Display.queryDecimalIndex(currentStackItem);

// If current stack item has more digits ahead of decimal make that the max - commas are included.
if (maxDigitsBeforeDecimal < decimalIndex) {
Expand All @@ -165,9 +165,9 @@ public static void main(String[] args) {

}

// Uncomment to debug alignment issues
// Output.debugPrintln("Alignment: Max digits before the decimal: " + maxDigitsBeforeDecimal);
// Output.debugPrintln("Alignment: Max length of longest item in stack: " + maxLenOfNumbers);
// Output information for alignment debugging
Output.debugPrintln("Alignment: Max digits before the decimal: " + maxDigitsBeforeDecimal);
Output.debugPrintln("Alignment: Max length of longest item in stack: " + maxLenOfNumbers);

// Display the current stack contents
for (int i = 0; i < calcStack.size(); i++) {
Expand All @@ -184,18 +184,17 @@ public static void main(String[] args) {
String stkLineNumber = String.format("%0" + LINE_NUMBER_DIGITS + "d: ", calcStack.size() - i);
Output.printColor(Ansi.Color.CYAN, stkLineNumber);

// Decimal Alignment - insert spaces before number to align to the decimal point
// DECIMAL ALIGNMENT: Insert spaces before number to align to the decimal point
if (configAlignment.compareTo("d") == 0) {
for (int k = 0; k < (maxDigitsBeforeDecimal - currentStackItem.indexOf(".")); k++) {
Output.print(" ");
}
int decimalIndex = Display.queryDecimalIndex(currentStackItem);

// Output the spaces in front so the decimals align
Output.print(" ".repeat(maxDigitsBeforeDecimal - decimalIndex));
}

// Right Alignment - insert spaces before number to right align
// RIGHT ALIGNMENT: Insert spaces before number to right align
if (configAlignment.compareTo("r") == 0) {
for (int k = 0; k < (maxLenOfNumbers - currentStackItem.length()); k++) {
Output.print(" ");
}
Output.print(" ".repeat(maxLenOfNumbers - currentStackItem.length()));
}

// Now that the spaces are inserted (for decimal/right) display the number
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/org/fross/rpncalc/DisplayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DisplayTest {

@Test
void testComma() {

assertEquals("1", Display.Comma(new BigDecimal("1")));
assertEquals("1.01", Display.Comma(new BigDecimal("1.01")));
assertEquals("-2.000000001231", Display.Comma(new BigDecimal("-2.000000001231")));
Expand All @@ -46,4 +44,17 @@ void testComma() {
assertEquals("-1,987,654,321,987,654,321", Display.Comma(new BigDecimal("-1987654321987654321")));
}

@Test
void testQueryDecimalIndex() {
assertEquals(6, Display.queryDecimalIndex("50,000"));
assertEquals(1, Display.queryDecimalIndex("1"));
assertEquals(1, Display.queryDecimalIndex("1.01"));
assertEquals(5, Display.queryDecimalIndex("-1234.4321"));
assertEquals(0, Display.queryDecimalIndex(".00001"));
assertEquals(18, Display.queryDecimalIndex("-1,123,454,678,999.1234532643"));
assertEquals(5, Display.queryDecimalIndex("-8e21"));
assertEquals(1, Display.queryDecimalIndex("1.02e+22"));
assertEquals(6, Display.queryDecimalIndex("-1e+22"));
}

}

0 comments on commit 4139a9d

Please sign in to comment.