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

Reply
Developer
Posts: 157
Registered: ‎03-29-2010
My Device: 8300
My Carrier: bb

Converting a double with four or one digit after decimal to one digit

Hi,

I want to convert a double variable with value like 4122.3132112 or 4122.0   what I need is after the decimal there should only Two Digits. I tried the below code and it did not work it was showing going to exception saying 

"java.lang.IllegalArgumentException: argument number too large at"

 

 

try
{
   double d = 4122.3132;
   MessageFormat mf = new MessageFormat("{#####.##}");
   Object[] objs = {new Double ( d )};
   String result = mf.format( objs );
   objs = null;   
}
catch(Exception e)
{}

 


 

 

Trusted Contributor
Posts: 145
Registered: ‎05-01-2011
My Device: 9780
My Carrier: Developer

Re: Converting a double with four or one digit after decimal to one digit

Do you really want to round off eg.. 4252.78852 to 4253 or you simply just want 4252.785858 to 4252.78 and 3258.7855 to 3258.78?

Highlighted
Trusted Contributor
Posts: 145
Registered: ‎05-01-2011
My Device: 9780
My Carrier: Developer

Re: Converting a double with four or one digit after decimal to one digit

This is for the second one (Not for round off)

 

try {
            double value = 4582.023242;
            final int numOfDig = 3;
            String strValue = String.valueOf(value);
            if(strValue.indexOf('.') != -1) {
                final int startIndex = strValue.indexOf('.');
                if(strValue.length() > startIndex + numOfDig) {
                    strValue = strValue.substring(0, startIndex + numOfDig);
                    value = Double.valueOf(strValue);
                }
            }
        } catch(final Exception e) {
            // Exception handling
        }

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

Re: Converting a double with four or one digit after decimal to one digit

I find it easier to convert the double to an appropriately rounded number, then use Double.toString(); 

 

But the short answer to your question is that there is no built-in formatting, aside from the Format class, and as you will find if you search this forum, that class only supports local dependent formatting, like "." or "," as the separator.

Developer
Posts: 157
Registered: ‎03-29-2010
My Device: 8300
My Carrier: bb

Re: Converting a double with four or one digit after decimal to one digit

Hi,

I want the output to be 4252.78

 

 

Trusted Contributor
Posts: 145
Registered: ‎05-01-2011
My Device: 9780
My Carrier: Developer

Re: Converting a double with four or one digit after decimal to one digit

Yes the above code will give the output as

 

If the input is 4252 then the output will be 4252

 

If input is 4252.0 then output will be 4252.0

 

if input is 4252.25 then output will be 4252.25

 

Here after it will cut off the digits added after decimal to only two digits (Eg., 4252.2525885 to 4252.25)

 

However if you need two digits after decimal point in all the cases i.e., 4252.0 will also be 4252.00 then change the above code according to that