Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 62
Registered: ‎01-22-2010
My Device: BOLD 9700
My Carrier: Globe
Accepted Solution

formatting numbers with comma

[ Edited ]

hi,

I'm using the 4.3 JDE. I'm trying to add commas to my Double.

Ex. 123456.1234 -> 123,456.12

 

I already tried searching the forum but i can't find an answer. I tried using this code:

 

Formatter formatter = new Formatter("en");

System.out.println(formatNumber(Double.parseDouble("123456.1234"), 2));

 

but i keep getting this -> 123456.12

Developer
Posts: 62
Registered: ‎01-22-2010
My Device: BOLD 9700
My Carrier: Globe

Re: formatting numbers with comma

anyone?

Developer
Posts: 62
Registered: ‎01-22-2010
My Device: BOLD 9700
My Carrier: Globe

Re: formatting numbers with comma

seriously.. i need help with these guys. anyone? please

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: formatting numbers with comma

I've noted this question being asked before.  I suggest you search the forum using the keyword formatter, for responses to similar questions.   

Developer
Posts: 62
Registered: ‎01-22-2010
My Device: BOLD 9700
My Carrier: Globe

Re: formatting numbers with comma

[ Edited ]

so based from this post:

 

http://supportforums.blackberry.com/t5/Java-Development/Formatting-a-floating-point-number/m-p/31802...

 

there's no way to add commas  from RIM's API?

Highlighted
Developer
Posts: 62
Registered: ‎01-22-2010
My Device: BOLD 9700
My Carrier: Globe

Re: formatting numbers with comma

[ Edited ]

Since there's no available method in Rim's API, I just constructed my own method for adding commas; if you have something better, something more optimized, please post it!

 

 

public String CommaSeparator(String nNum)
{
final static char DOT = 46;
growingstring="";
decimalString="";
if(Double.parseDouble(temp) >= 1000)
    {
//if it doesn't have a decimal portion
if(nNum.indexOf(DOT) == -1)
{
whatihave = nNum;
Length = whatihave.length();
}
else //else, split the string and put the decimal portion in
decimalString
{
decimalString = nNum.substring(nNum.indexOf(DOT),nNum.length());
whatihave = nNum.substring(0, nNum.indexOf(DOT));
Length = whatihave.length();
}

do
{
PartA = whatihave.substring(0, Length - 3);
PartB = whatihave.substring(Length - 3, Length);
growingstring = "," + PartB + growingstring;
whatihave = PartA;
Length = whatihave.length();
}while (Length > 3);

if (whatihave == "")
{
growingstring = growingstring.substring(1, growingstring.length());
}
else
{
growingstring = whatihave + growingstring;
}

if(decimalString != "")
{
gowingstring = growingstring + decimalString;
}

return growingstring;
}
return temp;
}

 

 

New Developer
Posts: 18
Registered: ‎07-17-2009
My Device: Not Specified

Re: formatting numbers with comma

Here's my version:

(I converted it to accept a string instead of a float.)

 

 

  // formatNumberString +++++++++++++++++++++++++++++++++++++++++++++++++
  //  Adds commas for thousands
  public static String formatNumberString( String sourceNumber ) {
    String result = "";
    int    DOT    = 0;
    String whole  = sourceNumber;
    String fract  = "";
    String segment = "";

    DOT = sourceNumber.indexOf( '.' );
    if ( DOT > -1 ) {
      whole = sourceNumber.substring( 0, DOT ); // integer portion
      fract = sourceNumber.substring( DOT, sourceNumber.length() );
 } // if while ( whole.length() > 3 ) { segment = whole.substring( whole.length() - 3, whole.length() ); whole = whole.substring( 0, whole.length() - 3 ); if ( result != "" ) { result = segment + "," + result; } // if else { result = segment; } // else } // while if ( result == "" ) { result = whole; } // if else { result = whole + "," + result; } // if return result + fract; } // formatNumberString ------------------------------------------------------