how to show 2 decimal places regardless of the number of digits after decimal in swift? -


i writing app in swift , have used below code . output rounded 2 decimal places expected.

this working well. if result less 2 decimal places shows 1 digit. basic example have results either whole number or 10 decimal places. want them show .xx

1.2345 => 1.23  1.1 => 1.1 

how results display 2 decimal places regardless of number of digits after decimal point ?

e.g: 1.1 => 1.10

i have searched extensively answer eludes me. code have tried far :

@iboutlet var odds: uitextfield! @iboutlet var oddslabel: uilabel! var enteredodds = nsstring(string: odds.text).doublevalue var numberofplaces = 2.0 var multiplier = pow(10.0, numberofplaces) var enteredoddsrounded = round(enteredodds * multiplier) / multiplier           oddslabel.text = "\(enteredoddsrounded)" println("\(enteredoddsrounded)") 

thanks comments. have amended follows:

@iboutlet var odds: uitextfield! @iboutlet var oddslabel: uilabel! var enteredodds = nsstring(string: odds.text).doublevalue  let formatter = nsnumberformatter() formatter.numberstyle = nsnumberformatterstyle.currencystyle  identifier in ["en_uk", "eu_us"] {     formatter.locale = nslocale(localeidentifier: identifier)     formatter.minimumfractiondigits = 2     formatter.maximumfractiondigits = 2 }  oddslabel.text = formatter.stringfromnumber(enteredodds) 

it allowed me lose lot of code rounding , decimal places me while including currency bonus.

thile has worked fields did need currency displayed, above value 'oddslabel.text' not currency , resolved rounding , decimal places.

how amend above can take account fields and/or without currency. without having re replicate code again?

thanks again quick replies.

frank

let b = 2.1 println(string(format:"%.02f", b)) 

gives string "2.10" in playground.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -