Skip to content

Commit

Permalink
add next BugFix for BigDecimal.setScale
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Dec 11, 2023
1 parent 396d450 commit f7dda52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Sources/JavApi/math/BigDecimal+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ extension java.math.BigDecimal {
case java.math.BigDecimal.ROUND_DOWN :
var down = ""
if newScale > 0 {
let plusScale = newScale * 1
var computedFactor = 10
for _ in 1..<plusScale { computedFactor *= 10 }
doubleValue = Double(Int (doubleValue * Double(computedFactor))) / Double(computedFactor)

down = String(format: "%.\(newScale)f" ,doubleValue)
}
else if newScale == 0 {
Expand Down
6 changes: 5 additions & 1 deletion Tests/JavApiTests/JavApi_math_BigDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ final class JavApi_math_BigDecimal_Tests: XCTestCase {
XCTAssertEqual(roundZero, java.math.BigDecimal.valueOf("1103802")!)
XCTAssertEqual(roundMinusOne, java.math.BigDecimal.valueOf("1.10380E+6")!)



// issue in 0.7.4 => scale > 0 & 5 as last
let onePoint55 = java.math.BigDecimal.valueOf("1.55")!
let onePoint5 = onePoint55.setScale(1, java.math.BigDecimal.ROUND_DOWN)
XCTAssertEqual(onePoint5, java.math.BigDecimal.valueOf("1.5")!)
}
}

0 comments on commit f7dda52

Please sign in to comment.