swift - Limiting fractional digits with NSNumberFormatter SpellOutStyle -


i'm trying create meaningful accessibility labels ios app , hitting think strange problem/limitation of nsnumberformatter.

import uikit  let decimal = 171.8926 let formatter = nsnumberformatter() formatter.numberstyle = .decimalstyle formatter.maximumfractiondigits = 2 print(formatter.stringfromnumber(decimal)!) // "171.89"  formatter.numberstyle = .spelloutstyle print(formatter.stringfromnumber(decimal)!) // "one hundred seventy-one point 8 9 2 six\n" 

it seems .spelloutstyle not honour maximumfractiondigits reason.

am missing or have truncate them myself animal?

let truncdecimal = double(int(decimal * 100)) / 100.0 print(formatter.stringfromnumber(truncdecimal)!) // "one hundred seventy-one point 8 nine\n" 

it seems crazy can use formatter strip digits numerics not words.

swift nsnumberformatter numberstyle spelloutstyle prints number out in full current locale.

to want recommend creating double extension:

extension double {     func roundtoplaces(places: int) -> double {         let divisor = pow(10.0, double(places))         return round(self * divisor) / divisor     } } 

and can call code this:

let decimal = 171.8976 let formatter = nsnumberformatter() formatter.numberstyle = .decimalstyle formatter.maximumfractiondigits = 2 print(formatter.stringfromnumber(decimal)!) // "171.89"  formatter.numberstyle = .spelloutstyle print(formatter.stringfromnumber(decimal.roundtoplaces(2))!) // "one hundred seventy-one point nine" 

Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -