J2me Limitations In Number Formatting - Can anyone provide a solution

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

J2me Limitations In Number Formatting - Can anyone provide a solution

darran
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?

Reply

githoko
QUOTE(darran @ Jan 7 2007, 03: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?


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?

Reply

darran
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?

Reply

githoko
QUOTE(darran @ Jan 8 2007, 02: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?



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

 

 

 


Reply

darran
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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. j2me formatting - 13.54 hr back. (1)
  2. j2me numberformat - 15.27 hr back. (1)
  3. j2me "number formatting" - 27.95 hr back. (1)
  4. j2me how to get number of digits in a number - 46.11 hr back. (1)
  5. integer in j2me - 47.51 hr back. (1)
  6. truncate a number in j2me - 54.43 hr back. (1)
  7. j2me string format decimal - 75.82 hr back. (1)
  8. j2me string formating - 106.69 hr back. (1)
  9. double formating j2me - 107.12 hr back. (1)
  10. double formatting j2me - 109.32 hr back. (1)
  11. number formatting java me - 110.84 hr back. (1)
  12. j2me limitations - 116.85 hr back. (1)
  13. java split after number of digits - 124.85 hr back. (1)
  14. j2me number to string - 133.68 hr back. (1)
Similar Topics

Keywords :

  1. J2me, Good Sites?
    Looking for resources. (3)


      Looking for J2me, Limitations, In, Number, Formatting

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for J2me, Limitations, In, Number, Formatting

*MORE FROM TRAP17.COM*
advertisement



J2me Limitations In Number Formatting - Can anyone provide a solution



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE