2

I have a float number that could be 3.553 or 34.535 or 353.6436 I want it to be no more than 5 figures, before or after the dot, for example, for the first number I wrote it has to be 3.5530, for the second 34.535 as is and for the third 353.64.

How can I do that with NSNumberFormatter?

ytpm
  • 4,962
  • 6
  • 56
  • 113

1 Answers1

2

Use NSNumberFormatter to convert the number to a string. Specify 5 decimal digits and turn off any grouping. Once you have the string, get the index of the decimal separator. If its index is 4 or less (a number less than 10,000), take the first 6 characters of the string. If its index is 5 or more (a number greater or equal to 10,000), take the first 5 characters of the string.

You may have to adjust this depending on how you want to deal with numbers less than 1 due to the leading zero ahead of the decimal separator.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I thought of something built in NSNumberFormatter, but that's works great, so why not? :) Thanks man! – ytpm Oct 25 '14 at 09:34