ios - Using NSNumberFormatter to generate number with trailing zeros after decimal -


Text after "div class =" itemprop = "text">

In SWIFT, how would you create a NSNumberFormatter that will protect zero follower after a decimal (

  Go Formatare = NSNumberFormatter () formatter.numberStyle = .DecimalStyle Formatter . Locale = NSLocale.currentLocale () formatter.maximumFractionDigits = 10 doubleNumString = "12.0" println (formatter.numberFromString (doubleNumString)) // In this print in English 12, it is 12.0 doubleNumString = "12.000" println (formattar want to print Printf (print number), numberFromString (doubleNumString)) // Print 12, want to print 12.125 print print ("formatter.numberFromString (doubleNumString)"), double.NumString = "12.125" to print 12.000 twice as doubleNumString = "1234" Println (formatter.numberFromString (doubleNumString)) // print as expected in print 1,234 print  

I already coded it If ng is finished in a decimal ( "12." ) then it will use this formater, not to generate the number and instead the number will display the decimal (but I think that Because some will need to improve, languages ​​are read left).

One solution would be to see if there is a duration in the string and if so, check that all the points behind it are 0, and if so, do not run it through it Number formatters and vice versa, then run the integer value through the formator / enlarge the decimal 0's.

Is there a better / cleaner solution?

After the proper number of

as described by Martin R, you can minimum fractions digit and maximum frameworks can be set to the same number which will always be displayed for many fraction digits. To know how many displays you need to take a substrings after decimals and calculate its elements to find out if all the numerical digits are 0 or not, I have created an assist method that is a sub - Turns the place into a number and if it is equal to 0, then you know that they were all 0.

Unfortunately you need to change the string for the localized number using a few different NSNumberFormatter s depending on the original string number. So if there is a decimal in it and then anything else then you need to create a different format, convert the string to a number, then convert that number into a string so that the respect of the user's locale can be displayed. Otherwise you can use your original number format.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -