iOS Question Extend DecimalNumber

Emme Developer

Well-Known Member
Licensed User
Longtime User
Hi everybody, i tried different times to extend the DecimalNumber class by @Erel, but without success. Anyone can help me to port this code? I'm not able to use very well Object c

B4X:
import Foundation

extension NSDecimalNumber {
    public func round(_ decimals:Int) -> NSDecimalNumber {
        return self.rounding(accordingToBehavior:
            NSDecimalNumberHandler(roundingMode: .plain,
                                   scale: Int16(decimals),
                                   raiseOnExactness: false,
                                   raiseOnOverflow: false,
                                   raiseOnUnderflow: false,
                                   raiseOnDivideByZero: false))
    }
}

let amount = NSDecimalNumber(string: "123.456")

Based on this answer
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add this sub to DecimalNumber class:
B4X:
Public Sub DecimalRound(NumberOfDigits As Int) As DecimalNumber
   Dim handler As NativeObject
   handler.Initialize("NSDecimalNumberHandler")
   handler = handler.RunMethod("decimalNumberHandlerWithRoundingMode:scale:raiseOnExactness:raiseOnOverflow:raiseOnUnderflow:raiseOnDivideByZero:", _
     Array(3, NumberOfDigits, False, False, False, False)) '3 = bankers rounding
   Dim dm As DecimalNumber
   dm.Initialize("")
   Return dm.InitWithDecimal(num.RunMethod("decimalNumberByRoundingAccordingToBehavior:", Array(handler)))
End Sub
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Damn @Erel, that's nice. I tried it 30times
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…