Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Rounding of Floats / String.valueOf(float)

807597Jul 26 2005 — edited Jul 28 2005
Hi all,

Here's my riddler....

I have a matrix that stores floats. These values are read in from a text file and have the form 1.21E-5 etc. These values can range anywhere but majority of them are from E-5 to E-15. I need to display these in pico (i.e. multiply by 10 to the minus 12):
 tabel[a][8]=(float)(tabel[a][8]*Math.pow(10.0,12.0)); //change energy to pico joules
So 1.21E-12 becomes 1.21 etc. This is all done and works fine.

The problem is with numbers like 1.04E-5 becomes something like 1.04E7. So now I can't use my function to round to two dp as this number is 10400000 etc.

So my question......

I need to have a function that takes a number like 1.04E7 and changes it to the full value as a string so you get 104000000 etc. As I have a function which changes numbers to 3 significant figures based on the string value. Previous I used the following for numbers that have negative powers (E-11) etc:

temp is the value i.e. 0.0000000000245641235 etc. Decimal format rounds to 0.00000000002456 then this string value is rounded to 3 sig figs to get 0.0000000000246
java.text.DecimalFormat df = new java.text.DecimalFormat("0.00000000000000");
    String tempnum = df.format(temp);   
    tabel[curlin][7]=sigfig3(tempnum);

public float sigfig3(String tempt){
    // function to set number (in string format) to 3 sig figs    
    String news="";
    int len = tempt.length(); // length of string
    int st=0; // sig figs
    int dp=0; // decimal point found
    
    for (int a=0;a<len;a++){
        int asc =(int) tempt.charAt(a);
        if (st<3){
            if ((asc != 48) & (dp==1)){  // if not 0 and point found
            news=news+tempt.charAt(a);
            st++;   
            }
            if ((asc == 48) & (dp==1)){ // if 0 and point found
            news=news+tempt.charAt(a);   
                if (st>0){
                    st++;
                }
            }
            
            if ((asc != 48) & (dp==0)){ // if not 0 and point not found
                if (asc==46){ // if decimal point...
                    dp=1;
                    news=news+tempt.charAt(a);
                }
                else{
                news=news+tempt.charAt(a);
                st++;
                }
            }   
            
        }
        else{
        news=news+"0";
        }
    }
    float newnum = Float.parseFloat(news);
        
    return newnum;    
    }
So basically I need to change 4.56E7 to 45600000 in order to make my function work.

Any help would be greatly appreciated.

Thanks,
John
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 25 2005
Added on Jul 26 2005
4 comments
377 views