We trying to do some operation with Rounding the values after X number of decimals. To do that We are using NSNumberFormatter
class. The results comes wrong.
Working JAVA Code
public static double roundOff(double value) {
DecimalFormat df2 = new DecimalFormat("#.##########");
df2.setRoundingMode(RoundingMode.HALF_EVEN);
return Double.parseDouble(df2.format(value));
}
public static double roundOff2(double value) {
DecimalFormat df2 = new DecimalFormat("#.###");
df2.setRoundingMode(RoundingMode.HALF_EVEN);
return Double.parseDouble(df2.format(value));
}
public static double roundOff2Step(double value) {
DecimalFormat df2 = new DecimalFormat("#.###");
df2.setRoundingMode(RoundingMode.HALF_EVEN);
return Double.parseDouble(df2.format(value));
}
The Swift Code We tried.
public func convertToRoundValue(number : Double)->Double{
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
formatter.roundingMode = .halfEven
let str = String(describing: formatter.string(from: NSNumber(value:number))!)
return str.toDouble()!
}
Is it possible to convert java code to swift? This calculations are purely based on Banking Sectors.
Example values :
8.60455187289436 to 8.61 In java code
8.60455187289436 to 8.60 In Swift code
My assumption :
//8.60455187289436
//8.60455187289444
//8.6045518728944
//8.604551872894
//8.60455187289
//8.6045518729
//8.604551873
//8.60455187
//8.6045519
//8.604552
//8.60455
//8.6046
//8.605
//8.61
//8.6
Thanks to All