Plus, the Float.parseFloat(String) method handles negative numbers and even
scientific notation, such as "2.0E-1".

-- Don
www.TheGadgetWorks.com


On Sun, Jun 20, 2010 at 7:39 AM,
<android-beginners+nore...@googlegroups.com<android-beginners%2bnore...@googlegroups.com>
> wrote:

>   Topic: StringToFloat 
> code<http://groups.google.com/group/android-beginners/t/318207b2a16e89c7>
>
>    kille6525 <yoda2...@gmail.com> Jun 19 09:02AM -0700 
> ^<#12955cd01de73397_digest_top>
>
>    Ok since the EditText.getText. will only allow you to get a string i
>    made a small program to get the string and convert it to a float. It
>    was a pain to do. You could change the return to an int or double but
>    i made this for floats...
>    Here is the horrible code...
>
>
>    public float StringToFloat(String text)
>    {
>    //Used for the for loops
>    int i = 0;
>    //The number your go to return
>    float number = 0;
>    //Used to specify if your before the decimal or after
>    int numtodec = 1;
>    //Size of the int. 123.6 sizeofint = 3
>    int sizeofint = -1;
>    //Gets the length of the String
>    int size = text.length();
>    //Sets an array of characters equ to the string
>    char string[] = text.toCharArray();
>    //Gets the size of the sizeofint and sizeofdec
>    for(i = 0; i < size; ++i)
>    {
>    if(string[i] == '.')
>    {
>    numtodec = 0;
>    continue;
>    }
>    if(numtodec == 1)
>    ++sizeofint;
>    }
>    //resets the num to dec
>    numtodec = 1;
>    //Stores the number
>    for(i = 0; i < size; ++i)
>    {
>    if(string[i] == '.')
>    {
>    numtodec = 0;
>    continue;
>    }
>    int hold = string[i] - 48;
>    double hold2;
>    if(numtodec == 1)
>    hold2 = java.lang.Math.pow(10.0,(double)(sizeofint - i));
>    else
>    hold2 = java.lang.Math.pow(10.0,(double)(sizeofint+1 - i));
>    number += hold*hold2;
>    }
>    return number;
>    }
>
>
>
>
>    Mikey <frak....@gmail.com> Jun 19 05:05PM +0100 
> ^<#12955cd01de73397_digest_top>
>
>    What was wrong with this:
>
>    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html
>
>
>    On 19 Jun 2010, at 17:02, kille6525 wrote:
>
>
>
>
>
>    kille6525 <yoda2...@gmail.com> Jun 19 10:31AM -0700 
> ^<#12955cd01de73397_digest_top>
>
>    There is no string to float function to do it for you. Now i dont have
>    it to where it will check for a negative or positive sign but in my
>    app there is none for now...
>
>
>
>
>
>    kille6525 <yoda2...@gmail.com> Jun 19 10:38AM -0700 
> ^<#12955cd01de73397_digest_top>
>
>    Oh yah i just tried the valueOf(String string) and it made my program
>    hate the two buttons one button caculates my two inputs and the other
>    finishes but when i added that in it didnt like any of them. Weird
>
>
>
>
>
>    Raymond Rodgers <raym...@badlucksoft.com> Jun 19 05:12PM -0400 
> ^<#12955cd01de73397_digest_top>
>
>    I think he was specifically referring to the parseFloat() function...
>    This is a direct link to a slightly different version of the api he
>    referenced...
>
>    
> http://java.sun.com/javase/7/docs/api/java/lang/Float.html#parseFloat(java.lang.String<http://java.sun.com/javase/7/docs/api/java/lang/Float.html#parseFloat%28java.lang.String>
>    )
>
>    Raymond
>    On 06/19/2010 01:38 PM, kille6525 wrote:
>
>    --
>    Raymond Rodgers
>    http://www.badlucksoft.com/
>
>
>
>
>    Mikey <frak....@gmail.com> Jun 19 10:14PM +0100 
> ^<#12955cd01de73397_digest_top>
>
>    That or the constructor that takes a string - but anything over making
>    your own parser...
>
>    On 19 Jun 2010, at 22:12, Raymond Rodgers wrote:
>
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to