IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

J2me Limitations In Number Formatting

, Can anyone provide a solution


darran
no avatar
Privileged Member
*********
Group: Members
Posts: 661
Joined: 31-August 06
Member No.: 29,189
myCENT:ZERO



Post #1 post Jan 7 2007, 01:43 PM
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?
Go to the top of the page
+Quote Post
githoko
no avatar
Newbie [Level 1]
*
Group: Members
Posts: 11
Joined: 5-January 07
Member No.: 36,592



Post #2 post Jan 8 2007, 08:00 AM
QUOTE(darran @ Jan 7 2007, 03:43 PM) [snapback]302752[/snapback]

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?
Go to the top of the page
+Quote Post
darran
no avatar
Privileged Member
*********
Group: Members
Posts: 661
Joined: 31-August 06
Member No.: 29,189
myCENT:ZERO



Post #3 post Jan 8 2007, 12:41 PM
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?
Go to the top of the page
+Quote Post
githoko
no avatar
Newbie [Level 1]
*
Group: Members
Posts: 11
Joined: 5-January 07
Member No.: 36,592



Post #4 post Jan 10 2007, 06:31 AM
QUOTE(darran @ Jan 8 2007, 02:41 PM) [snapback]302937[/snapback]

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

Go to the top of the page
+Quote Post
darran
no avatar
Privileged Member
*********
Group: Members
Posts: 661
Joined: 31-August 06
Member No.: 29,189
myCENT:ZERO



Post #5 post Jan 11 2007, 12:57 PM
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
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new 22 spawn_syxx9 11,459 16th January 2005 - 08:29 PM
Last post by: spawn_syxx9
No New Posts   2 Sanguinairius 3,443 7th October 2004 - 04:26 PM
Last post by: wassie
No New Posts 0 Soviet 662 15th October 2007 - 05:18 AM
Last post by: Soviet
No New Posts   3 sargilla 3,906 6th January 2005 - 11:25 AM
Last post by: sargilla
No new   23 Xedos 5,593 22nd February 2007 - 09:21 AM
Last post by: TypoMage
No New Posts   4 Amby 3,463 18th February 2005 - 05:54 AM
Last post by: XeNoToSs
No New Posts   10 thek2kid 4,714 16th April 2009 - 09:43 AM
Last post by: iG-lester
No New Posts   1 nitheesh 510 28th August 2007 - 03:30 AM
Last post by: jlhaslip
No New Posts   2 iwuvcookies 899 30th July 2006 - 04:52 PM
Last post by: lawmatchmaker
No New Posts   1 -Gladwin- 1,634 18th December 2004 - 03:46 AM
Last post by: -sandman-
No New Posts   4 beeseven 3,097 18th April 2005 - 12:38 AM
Last post by: snlildude87
No new   63 lil_chris 8,082 1st June 2007 - 01:33 AM
Last post by: itrainmonkeys
No New Posts   3 kvarnerexpress 3,313 7th December 2008 - 06:39 PM
Last post by: iG-Ahmad Sheeraz Saeed
No New Posts   8 Inspiron 1,606 5th January 2007 - 03:03 AM
Last post by: michaelper22
No New Posts   11 Cain 1,479 24th June 2006 - 07:12 AM
Last post by: AlternativeNick


 



RSS Open Discussion Time is now: 26th November 2009 - 05:24 PM

Web Hosting Powered by ComputingHost.com. Xisto.com : Honesty ROCKS! Truth Rules.