Skip to content

Commit

Permalink
move static constants to BigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Dec 10, 2023
1 parent f4eece0 commit c758c48
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
8 changes: 7 additions & 1 deletion Sources/JavApi/math/BigDecimal+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import Foundation

extension java.math.BigDecimal {

public static let ZERO = java.math.BigDecimal(0.0)
public static let ONE = java.math.BigDecimal(1.0)
public static let ROUND_UP = 0
public static let ROUND_DOWN = 1

public func setScale (_ newScale : Int, _ roundingMode : Int) -> java.math.BigDecimal {
let before = self.exponent
var stringValue = ""
if before < 0 { // -2 = to chars after point
stringValue = "\((self as NSDecimalNumber).doubleValue)"
stringValue = "\(self)"

// trim to scale
var newStringValue = ""
Expand Down
5 changes: 0 additions & 5 deletions Sources/JavApi/math/Decimal+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import Foundation

extension Decimal {

public static let ZERO = java.math.BigDecimal(0)
public static let ONE = java.math.BigDecimal(1)
public static let ROUND_UP = 0
public static let ROUND_DOWN = 1

// exponent is the internal field of accurance

/// NOTE: Same as Java is a bad idea using a double to initialize a BigDecimal value. Better use ``valueOf:(String)`` because the internal information exponent is set correct
Expand Down
11 changes: 10 additions & 1 deletion Tests/JavApiTests/JavApi_math_BigDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,14 @@ final class JavApi_math_BigDecimal_Tests: XCTestCase {

XCTAssertEqual((KVSATZAN as NSDecimalNumber).stringValue, "0.07")
}


public func testScale () {
/// see pseudo code from German Federal Ministry of Finance [income tax calculator](https://www.bmf-steuerrechner.de/interface/einganginterface.xhtml)
var VSP1 = java.math.BigDecimal.valueOf("25000")!
let RVSATZAN = java.math.BigDecimal.valueOf("0.093000")!

VSP1 = (VSP1.multiply(RVSATZAN)).setScale(2,java.math.BigDecimal.ROUND_DOWN)
XCTAssertEqual(VSP1, java.math.BigDecimal.valueOf("2325.00")!)

}
}

0 comments on commit c758c48

Please sign in to comment.