Hello,
I want to change
14201
to
14.2k
using NumberFormat
How can i do this?
Number Formatting #2
Started by dunnkers, Aug 23 2010 02:28 PM
9 replies to this topic
#1
Posted 23 August 2010 - 02:28 PM
|
|
|
#2
Posted 23 August 2010 - 02:49 PM
I've a little start:
formatter = new DecimalFormat("#'k'");
s = formatter.format(14201); // 14201k
formatter = new DecimalFormat("#'k'");
s = formatter.format(14201); // 14201k
#3
Posted 23 August 2010 - 03:54 PM
bump.. anyone?
#4
Posted 23 August 2010 - 04:03 PM
You could start by looking at it as a string. The length of the string tells you how many characters you need before the new decimal.
#5
Posted 23 August 2010 - 04:07 PM
That will result in a long if statement maze.
#6
Posted 23 August 2010 - 04:13 PM
Why would it do that? Are you supporting more suffixes than k? It's mostly a little math.
#7
Posted 23 August 2010 - 05:44 PM
Can you give me an example please?
#8
Posted 23 August 2010 - 11:26 PM
I don't think you can divide by 1000 to drop the last numbers with the formatting.
A short version would be:
What wingedpanther wanted to do would be something like the following i guess:
A short version would be:
NumberFormat formatter = new DecimalFormat("#0.#k");
System.out.println(formatter.format(14201.0/1000.0));
But you have to do /1000.0 yourself (.0 because it has to be doubles)What wingedpanther wanted to do would be something like the following i guess:
int number = 12345; double doubleNumber = number; doubleNumber /= 100; doubleNumber = Math.round(doubleNumber); doubleNumber /= 10; System.out.println(doubleNumber + "k");
#9
Posted 24 August 2010 - 12:16 PM
I'd convert it to a string first. Then display the first n-3 characters, a decimal, and the last of the n-2 characters, and a "k".
#10
Posted 25 August 2010 - 01:02 AM
Ah ye, probably even easier :D


Sign In
Create Account


Back to top









