Jump to content
xisto Community
darran

J2me Limitations In Number Formatting Can anyone provide a solution

Recommended Posts

I am doing a J2ME application which involves the mathematical manipulation of datatypes integer and double. So the end result would be something like 1.2856 and I would like to round it or truncate, whichever you call it to something along the lines of 1.29. I have tried looking around but realised in J2ME I can't make use of the NumberFormat package to handle this like I do in J2SE.Can anyone provide an alternative solution?

Share this post


Link to post
Share on other sites

I am doing a J2ME application which involves the mathematical manipulation of datatypes integer and double. So the end result would be something like 1.2856 and I would like to round it or truncate, whichever you call it to something along the lines of 1.29. I have tried looking around but realised in J2ME I can't make use of the NumberFormat package to handle this like I do in J2SE.
Can anyone provide an alternative solution?


Have you tried the concrete implementation of NumberFormat, DecimalFormat in java.text, or by you can't make use of NumberFormat, you mean all it's subclasses?

Share this post


Link to post
Share on other sites

I can't use java.text, it is not an available package for me to make use of. Is there anything else I could do?



String number = String.valueOf(2.34566);//turn number to string

String afterDecimalPoint = number.split("(.)")[1];//split the number after the decimal point
//and take the digits after that.
number = afterDecimalPoint;

number = (number.length() > 4 ? number.substring(0,4) : number);//cut it to the number of digits you
//want plus one.

char[] digits = number.toCharArray();//split it into a char array containing a single digit at each index

//this part is an attempt to round up the number.

int lastDigitsValue = new Integer(new String(digits[3])).intValue();//convert last digit to int.


if(lastDigitsValue > 4) { //if it's bigger than four,i.e if it 5 and upwards then

int secondLastDigitsValue = new Integer(new String(digits[2])).intValue(); //get the second last digit

digits[2] = new String((++secondLastDigitsValue)+"").toCharArray()[0]; // increase it by 1;

}

String finalNumber = digits[0]+digits[1]+digits[2]; //Put them back together without the extra digit.

if you want : double numb = Double.parseDouble(finalNumber); //to get it back to being a double

Share this post


Link to post
Share on other sites

I didn't think a coding of a method was necessary for a simple arithmetic function such as rounding up to 2 decimal points. But nonetheless, I will try this set of code over the weekend when I have more time to spare. Thanks for the effort though

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.