NSNumberFormatter to display custom labels for 10^n (10000 -> 10k)

Posted by Michele Colombo on Stack Overflow See other posts from Stack Overflow or by Michele Colombo
Published on 2010-04-27T08:49:07Z Indexed on 2010/04/27 8:53 UTC
Read the original article Hit count: 323

I need to display numbers on a plot axis. The values could change but I want to avoid too long numbers that will ruin the readability of the graph. My thought was to group every 3 characters and substitute them with K, M and so on (or a custom character). So: 1 -> 1, 999 -> 999, 1.000 -> 1k, 1.200 -> 1.2k, 1.280 -> 1.2k, 12.800 -> 12.8k, 999.999 -> 999.9k, 1.000.000 -> 1M, ...

Note that probably I'll only need to format round numbers (1, 10, 1000, 1500, 2000, 10000, 20000, 30000, 100000, ...). Is that possibile with NSNumberFormatter? I saw that it has a setFormat method but I don't know how much customizable it is.

I'm using NSNumberFormatter cause the graph object I use wants it to set label format and I want to avoid changing my data to set the label.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone