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

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -