lightmaster2006 lightmaster2006 - 1 year ago 104
iOS Question

Removing numbers using NSnumberformatter from a calculated number

I have a calculation that calculate square meters from millimeters and for this to show corect I would like to remove all numbers after the 2 first digits.

This is what I have tried so far.

let spha = sizeh.wshight()
let spwa = sizew.wswidth()
let areas1 = areaModel(pwa:spwa, pha:spha)

let formatter5 = NSNumberFormatter ()
formatter5.maximum = 2
let scarea = formatter5.stringFromNumber(areas1.sarea())!

screenAreaLabel?.text = "\(scarea)sqm"

I am a little lost at this point, I am using Float in calculation of this area.

Hope someone could help me in the right direction.

More detail info added.

Image of app

I would like the Screen Area to only show 24sqm not 24772610sqm

Answer Source

You are multiplicating a value in millimeter by anothe value in milliters, therefore the result is in square millimeters.

To convert square millimeters to square meters, just divide the result by 1_000_000.

You could also set formatter.multiplier to 1.0 / 1_000_000.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download