In the CLDC 1.0 configuration, there is no method for converting float to string. So I had to write myself. It is not a question at all, it may just be useful to someone. Unit tests did not, but in my application works without bugs.

  • the first parameter is the number [3233 millivolts]
  • second how many times divide by 10 [3]
  • the third parameter is accuracy [1]

As a result, we get the string "3.2" or with the parameters -54 1 3 will be -5.400.

public static String GetFloat(int val, int div, int pres){ StringBuffer sb=new StringBuffer(); char[] schars; if (val>=0){ schars=String.valueOf(val).toCharArray(); }else{ sb.append("-"); schars=String.valueOf(-val).toCharArray(); } if (schars.length<=div){ int i; int div_cnt=div-schars.length+1; sb.append("0."); for (i=1;i<div_cnt;i++){ sb.append("0"); } int zeros=i-1; if (zeros<pres){ i=0; while(zeros+i<pres){ if (i>=schars.length){ sb.append("0");i++; }else{ sb.append(schars[i++]); } } } }else{ int prs=0; int i=0;//schars ptr int pointAfter=schars.length-div; while(i<pointAfter){ if (i<schars.length) sb.append(schars[i++]); } sb.append("."); while(prs<pres) { if (i<schars.length){ sb.append(schars[i++]); }else{ sb.append("0"); } prs++; } } return sb.toString(); } 

    1 answer 1

    And what you did not like BigDecimal?

     new BigDecimal(5.243454).setScale(2,BigDecimal.ROUND_FLOOR).toPlainString() 

    Displays "5.24"