iOS Question Extend DecimalNumber

Emme Developer

Well-Known Member
Licensed 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

Administrator
Staff member
Licensed 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
 

Emme Developer

Well-Known Member
Licensed 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
Damn @Erel, that's nice. I tried it 30times :(
 
Top