Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test case for the multiplication bug - currently failing. #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/info/javaperformance/money/MoneyLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public Money multiply( long multiplier ) {
if ( origUnits != m_units )
{
final BigInteger res = BigInteger.valueOf( m_units ).multiply( BigInteger.valueOf( multiplier ) );
return MoneyFactory.fromBigDecimal( new BigDecimal( res ) );
return MoneyFactory.fromBigDecimal( new BigDecimal( res, m_precision ) );
}
return new MoneyLong( resUnits, m_precision ).normalize();
}
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/info/javaperformance/money/MoneyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,27 @@ public void testTicket11()
final Money res = total.subtract( x );
assertNotNull( res ); //simply should not throw an exception
}


public void testMultiplicationIssue14()
{
// allocation is 30%
double allocation = 30;
// total value
Money totalValue = MoneyFactory.fromString("7281.6612");
// money value of 30% allocation
double value = allocation * totalValue.toDouble() / 100;
Money moneyValue = MoneyFactory.fromDouble(value);

// calculate the percentage of the money value
double currentAllocationD = moneyValue.multiply(100)
.divide(totalValue.toDouble(), 4)
.toDouble();

Money currentAllocation = moneyValue.multiply(100)
.divide(totalValue.toDouble(), 4);

// it should be 30, as set initially.
assertEquals( 30d, currentAllocationD);
assertEquals( "30", currentAllocation.toString());
}
}