Conversion string to float or int

Hi,
I have a value "3,000.00 INR" in String type variable.
Can I get the numeric value i.e. 3,000 in number or float type variable. Because I have to comparison after that with this value.
Pls give me the solution ASAP.
Its really urgent.
Thanks for ur help
Rahul

Hi
844851 wrote:
I have a value "3,000.00 INR" in String type variable.
Can I get the numeric value i.e. 3,000 in number or float type variable. Because I have to comparison after that >with this value.---we can convert String to int using Integer.parseInt .
int projectId=Integer.parseInt(project_id);---project_id is String var..
Regards
Meher Irk

Similar Messages

  • Converting string to float

    I have a decimal string "122339994" which i am trying to convert to float using Float.parseFloat. This results in incorrect value of 1.22339992E8.
    I thought for floats precsision comes into effect after decimal places.
    public static void main(String[] args) {
    String floatString = "122339994";
    float floatNumber = Float.parseFloat(floatString);
    System.out.println("Float is "+floatNumber+". Now double "+Double.valueOf(floatString));
    }

    See this API
    [Java2SE Float|http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Float.html#valueOf%28java.lang.String%29]
    Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
    "1.00000017881393421514957253748434595763683319091796875001d"
    results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results.
    Its better to see the Java APIs first for any information, we will get almost all the information we need from APIs
    Regards,
    Venkatesh

  • JAVA Float to int exception

    Hi
    I am facing a serious problem after migrating application from oracle 9i to oracle 10g.
    Application was earlier connected with oracle 9i as database after migration to 10g .
    I am facing a problem of float to int exception.
    In database,in table where a column's datatype is defined as number() is treated in java as float but
    *number(p)[p any integer]* is treated as integer in java.
    Earlier when application was on Oracle 9i i never faced this problem but in oracle 10g it is present.
    Can some one help me in this issue.
    What should i do so that data type number() is also treated as integer in java

    You could change your code, or change at table level.
    This is not a trivial task, though:
    SQL> create table t(x number)
    Table created.
    SQL> insert into t values (1)
    1 row created.
    SQL> commit
    Commit complete.
    SQL> alter table t modify x number(10)
    alter table t modify x number(10)
    Error at line 8
    ORA-01440: column to be modified must be empty to decrease precision or scaleYou need something like:
    SQL> alter table t add y number (10)
    Table altered.
    SQL> update t set y = x, x=null
    1 row updated.
    SQL> commit
    Commit complete.
    SQL> alter table t drop column x
    Table altered.
    SQL> alter table t rename column y to x
    Table altered.
    SQL> select * from t
             X
             1
    1 row selected.Regards
    Peter

  • How do I know if I can convert a String value to an int value or not?

    Hi,
    I want to know how to make the judgment that if I can convert a String value to an int value or not? Assume that I don't know the String is number or letters
    Thank you

    Encephalopathic wrote
    Again, why?One of the problems (have been dued) in my codelab asks us to write a class as follow
    Write a class definition of a class named 'Value' with the following:
    a constructor accepting a single integer paramter
    a constructor with no parameters
    a method 'setVal' that accepts a single parameter,
    a boolean method, 'wasModified' that returns true if setVal was ever called for the object.
    a method 'getVal' that returns an integer value as follows: if setVal has ever been called, it getVal returns the last value passed to setVal. Otherwise if the "single int parameter" constructor was used to create the object, getVal returns the value passed to that constructor. Otherwise getVal returns 0.
    The setVal(int y) returns nothing, so how do I know whether it has been called or not?
    Thank you

  • Convert from String to float

    How do I convert from String to float?

    Hi,
    you can use a Double for example - assuming value is that string to parse
    float f;
    try { Double d = new Double(value); f = d.floatValue(); }
    catch (NumberFormatException e) { f = 0.0; } // error - string value could not be parsed
    // here use your float fHope, that helps
    greetings Marsian
    P.S.: the Double class is usefull for that, because you also can get intValue(), doubleValue() or longValue() out of it for example. The StreamTokenizer for example parses numbers also only to double.

  • Convert string to floating-point

    Hi all,
    ..very basic question, but I tryed it for hours and only received short-dumps
    <b>How can I convert a string into a floating-point number?</b>
    Kind regards,
    Stefan

    hi
    try this
    to convert  string to float.
    data : a type f,
    s type string value '1.023'.
    a = s.
    write :/ a.
    to convert float to string.
    data : a type f value '1.023',
    s type string.
    s = a.
    write : s.

  • How to change string to float...??

    sorie to trouble u ppls..
    but i cant get to change the string to float...
    mi use
    amt[i] = Float.parseFloat(input);
    amt is a float declared as : float amt[] = new float[3];
    and input is declared as : String input;
    then the 'i' is jus a counter...
    pls help mi ....
    christopher.

    wait that was formatting wrong...
    amt[ i ] = Float.parseFloat(input);

  • I tried to text a photo from i phone 5 to a contact in i messaging.  When the contact came up, a conversation string that had been deleted also came up...I've not see this before.  Where might this conversation string be stored?  Thank you

    I tried to text a photo from i phone 5 to a contact in i messaging.  When the contact came up, a conversation string that had been deleted also came up...I've not see this before.  Where might this conversation string be stored?  Thank you

    I tried to text a photo from i phone 5 to a contact in i messaging.  When the contact came up, a conversation string that had been deleted also came up...I've not see this before.  Where might this conversation string be stored?  Thank you

  • Replacing String month with type Int - What to do!?!

    import java.util.Scanner;
    public class Date
        private String month;
        private int day;
        private int year; //a four digit number.
        public Date( )
            month = "January";
            day = 1;
            year = 1000;
        public Date(int monthInt, int day, int year)
            setDate(monthInt, day, year);
        public Date(String monthString, int day, int year)
            setDate(monthString, day, year);
        public Date(int year)
            setDate(1, 1, year);
        public Date(Date aDate)
            if (aDate == null)//Not a real date.
                 System.out.println("Fatal Error.");
                 System.exit(0);
            month = aDate.month;
            day = aDate.day;
            year = aDate.year;
        public void setDate(int monthInt, int day, int year)
            if (dateOK(monthInt, day, year))
                this.month = monthString(monthInt);
                this.day = day;
                this.year = year;
            else
                System.out.println("Fatal Error");
                System.exit(0);
        public void setDate(String monthString, int day, int year)
            if (dateOK(monthString, day, year))
                this.month = monthString;
                this.day = day;
                this.year = year;
            else
                System.out.println("Fatal Error");
                System.exit(0);
        public void setDate(int year)
            setDate(1, 1, year);
        public void setYear(int year)
            if ( (year < 1000) || (year > 9999) )
                System.out.println("Fatal Error");
                System.exit(0);
            else
                this.year = year;
        public void setMonth(int monthNumber)
            if ((monthNumber <= 0) || (monthNumber > 12))
                System.out.println("Fatal Error");
                System.exit(0);
            else
                month = monthString(monthNumber);
        public void setDay(int day)
            if ((day <= 0) || (day > 31))
                System.out.println("Fatal Error");
                System.exit(0);
            else
                this.day = day;
        public int getMonth( )
            if (month.equals("January"))
                return 1;
            else if (month.equals("February"))
                return 2;
            else if (month.equalsIgnoreCase("March"))
                return 3;
            else if (month.equalsIgnoreCase("April"))
                return 4;
            else if (month.equalsIgnoreCase("May"))
                return 5;
            else if (month.equals("June"))
                return 6;
            else if (month.equalsIgnoreCase("July"))
                return 7;
            else if (month.equalsIgnoreCase("August"))
                return 8;
            else if (month.equalsIgnoreCase("September"))
                return 9;
            else if (month.equalsIgnoreCase("October"))
                return 10;
            else if (month.equals("November"))
                return 11;
            else if (month.equals("December"))
                return 12;
            else
                System.out.println("Fatal Error");
                System.exit(0);
                return 0; //Needed to keep the compiler happy
        public int getDay( )
            return day;
        public int getYear( )
            return year;
        public String toString( )
            return (month + " " + day + ", " + year);
        public boolean equals(Date otherDate)
            return ( (month.equals(otherDate.month))
                      && (day == otherDate.day) && (year == otherDate.year) );
        public boolean precedes(Date otherDate)
            return ( (year < otherDate.year) ||
               (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
               (year == otherDate.year && month.equals(otherDate.month)
                                             && day < otherDate.day) );
        public void readInput( )
            boolean tryAgain = true;
            Scanner keyboard = new Scanner(System.in);
            while (tryAgain)
                System.out.println("Enter month, day, and year.");
                  System.out.println("Do not use a comma.");
                String monthInput = keyboard.next( );
                int dayInput = keyboard.nextInt( );
                int yearInput = keyboard.nextInt( );
                if (dateOK(monthInput, dayInput, yearInput) )
                    setDate(monthInput, dayInput, yearInput);
                    tryAgain = false;
                else
                    System.out.println("Illegal date. Reenter input.");
        private boolean dateOK(int monthInt, int dayInt, int yearInt)
            return ( (monthInt >= 1) && (monthInt <= 12) &&
                     (dayInt >= 1) && (dayInt <= 31) &&
                     (yearInt >= 1000) && (yearInt <= 9999) );
        private boolean dateOK(String monthString, int dayInt, int yearInt)
            return ( monthOK(monthString) &&
                     (dayInt >= 1) && (dayInt <= 31) &&
                     (yearInt >= 1000) && (yearInt <= 9999) );
        private boolean monthOK(String month)
            return (month.equals("January") || month.equals("February") ||
                    month.equals("March") || month.equals("April") ||
                    month.equals("May") || month.equals("June") ||
                    month.equals("July") || month.equals("August") ||
                    month.equals("September") || month.equals("October") ||
                    month.equals("November") || month.equals("December") );
        private String monthString(int monthNumber)
            switch (monthNumber)
            case 1:
                return "January";
            case 2:
                return "February";
            case 3:
                return "March";
            case 4:
                return "April";
            case 5:
                return "May";
            case 6:
                return "June";
            case 7:
                return "July";
            case 8:
                return "August";
            case 9:
                return "September";
            case 10:
                return "October";
            case 11:
                return "November";
            case 12:
                return "December";
            default:
                System.out.println("Fatal Error");
                System.exit(0);
                return "Error"; //to keep the compiler happy
    }My question is that if I were to change the String month in the instance variables to type int, what changes do I make to the code to make this work WITHOUT changing the method headings and it also says that none of the type String paraenters should change to type int. I need to redefine the methods to make it work. I've been at this for hours and am just stuck so this is the reason for my early morning post.

    import java.util.Scanner;
    public class Date
        private int month;
        private int day;
        private int year; //a four digit number.
        public Date( )
            month = 1;
            day = 1;
            year = 1000;
        public Date(int monthInt, int day, int year)
            setDate(monthInt, day, year);
        public Date(String monthString, int day, int year)
            setDate(monthString, day, year);
        public Date(int year)
            setDate(1, 1, year);
        public Date(Date aDate)
            if (aDate == null)//Not a real date.
                 System.out.println("Fatal Error.");
                 System.exit(0);
            month = aDate.month;
            day = aDate.day;
            year = aDate.year;
        public void setDate(int monthInt, int day, int year)
            if (dateOK(monthInt, day, year))
                this.month = monthInt;
                this.day = day;
                this.year = year;
            else
                System.out.println("Fatal Error");
                System.exit(0);
        public void setDate(String monthString, int day, int year)
            if (dateOK(monthString, day, year))
                this.month = monthStringToInt(monthString);
                this.day = day;
                this.year = year;
            else
                System.out.println("Fatal Error");
                System.exit(0);
        public void setDate(int year)
            setDate(1, 1, year);
        public void setYear(int year)
            if ( (year < 1000) || (year > 9999) )
                System.out.println("Fatal Error");
                System.exit(0);
            else
                this.year = year;
        public void setMonth(int monthNumber)
            if ((monthNumber <= 0) || (monthNumber > 12))
                System.out.println("Fatal Error");
                System.exit(0);
            else
                month = monthNumber;
        public void setDay(int day)
            if ((day <= 0) || (day > 31))
                System.out.println("Fatal Error");
                System.exit(0);
            else
                this.day = day;
        public int getMonth( )
             return month;
        public int getDay( )
            return day;
        public int getYear( )
            return year;
        public String toString( )
            return (month + " " + day + ", " + year);
        public boolean equals(Date otherDate)
            return ( (month == otherDate.month)
                      && (day == otherDate.day) && (year == otherDate.year) );
        public boolean precedes(Date otherDate)
            return ( (year < otherDate.year) ||
               (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
               (year == otherDate.year && month == otherDate.month
                                             && day < otherDate.day) );
        public void readInput( )
            boolean tryAgain = true;
            Scanner keyboard = new Scanner(System.in);
            while (tryAgain)
                System.out.println("Enter month, day, and year.");
                  System.out.println("Do not use a comma.");
                String monthInput = keyboard.next( );
                int dayInput = keyboard.nextInt( );
                int yearInput = keyboard.nextInt( );
                if (dateOK(monthInput, dayInput, yearInput) )
                    setDate(monthInput, dayInput, yearInput);
                    tryAgain = false;
                else
                    System.out.println("Illegal date. Reenter input.");
        private boolean dateOK(int monthInt, int dayInt, int yearInt)
            return ( (monthInt >= 1) && (monthInt <= 12) &&
                     (dayInt >= 1) && (dayInt <= 31) &&
                     (yearInt >= 1000) && (yearInt <= 9999) );
        private boolean dateOK(String monthString, int dayInt, int yearInt)
            return ( monthOK(monthString) &&
                     (dayInt >= 1) && (dayInt <= 31) &&
                     (yearInt >= 1000) && (yearInt <= 9999) );
        private boolean monthOK(String month)
            return (month.equals("January") || month.equals("February") ||
                    month.equals("March") || month.equals("April") ||
                    month.equals("May") || month.equals("June") ||
                    month.equals("July") || month.equals("August") ||
                    month.equals("September") || month.equals("October") ||
                    month.equals("November") || month.equals("December") );
        private String monthString(int monthNumber)
            switch (monthNumber)
            case 1:
                return "January";
            case 2:
                return "February";
            case 3:
                return "March";
            case 4:
                return "April";
            case 5:
                return "May";
            case 6:
                return "June";
            case 7:
                return "July";
            case 8:
                return "August";
            case 9:
                return "September";
            case 10:
                return "October";
            case 11:
                return "November";
            case 12:
                return "December";
            default:
                System.out.println("Fatal Error");
                System.exit(0);
                return "Error"; //to keep the compiler happy
        private int monthStringToInt(String monthString) {
             if (monthString.equals("January")) return 1;
             else if (monthString.equals("February")) return 2;
             else if (monthString.equals("March")) return 3;
             else if (monthString.equals("April")) return 4;
             else if (monthString.equals("May")) return 5;
             else if (monthString.equals("June")) return 6;
             else if (monthString.equals("July")) return 7;
             else if (monthString.equals("August")) return 8;
             else if (monthString.equals("September")) return 9;
             else if (monthString.equals("October")) return 10;
             else if (monthString.equals("November")) return 11;
             else if (monthString.equals("December")) return 12;
             else return -1;
    }I've added one method: monthStringToInt to convert "January", "February" into an integer value. There are probably better ways to do this (Calendar), but this involes no other classes.

  • Why findByAltKey(String keyName, Key key, int) returns a RowIterator object

    Hi All,
    I was thinking that why findByAltKey(String keyName, Key key, int) returns a RowIterator object and not Row object because alternate key is a candidate key and is unique so only one record should be there for a particular alternate key.
    Thanks
    Puneet

    findByKey (which you could also use) returns Row[]. findByAltKey is just a special version of findByKey that returns a RowIterator.
    Probably it's not going to change for historical reasons.
    Plus, the Key you use doesn't have to be a full key - it can be a partial one, meaning that you can match more than one row.
    John

  • Conversion Vector of Floats to float[]:  exception?

    Java Developers,
    I am able to convert my String vector to a
    String[] but just can't get the Vector with Floats to be converted to a float[] array. I have looked into google and java.sun.com Forums but still cant seem to find the answer. I would really appreciate your help!
    This is how I am making the conversion from Vector(String) to String[]:
    legendLabelsArray = new String[columnHeads.size()];
    int k=0;
    Iterator e = columnHeads.iterator();
    while(e.hasNext()){
    System.out.println("inside the enumerator");
    legendLabelsArray[k] = e.next().toString();
    System.out.println("Array elements at " + k + " are " + legendLabelsArray[k]);
    System.out.println(k++);
    How can I make something similar to work with a Vector with Floats to float[] instead of Strings?
    Thanks,
    Musaddiq
    [email protected]

    Assuming Vector v with Float elements:
    float[] a = new float[v.size()];
    int n = 0;
    for (Iterator i = v.iterator(); i.hasNext(); )
        a[n++] = ((Float) i.next()).floatValue();

  • Converting from String to float and vice versa

    I'm interested in people's thoughts on where common logic to convert between various field types should be stored in an application to minimise code duplication and maintenance.
    I have an application which consists of an object and a jPanel which displays and maintains this object. The object contains a number of private float fields which are accessed by getters and setters. The jPanel contains one jTextField for each of the fields within the object.
    Currently I have numerous lines of code in the jPanel to convert between the values needed by the getters and setters in the object (i.e. float) and the String value used by the jTextFields. This code handles cases where the String value may be blank or null.
    I've thought that one alternative to having all this conversion/validation code in the jPanel is to create a second set of getters and setters for each field which accept and return String values.
    What do people think about this? Is it advisable for only have one getter and setter for a variable? Should I put the conversion/validation logic for each field into a seperate common routine?
    Thanks,
    James.

    Hi James,
    You should go with whatever works best for you. By creating multiple getters and setter you save yourself from repeating the same code throughout your program.

  • Annoying String to Float in AS3

    Hey again,
    I am working on *AS3* here.
    Problem: I have a value stored in a String. I'd like to check
    whether
    that String is a float. I am aware of the method
    parseFloat(string)
    described here:
    http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/
    actionscript_dictionary620.html
    However, this method has annoying limitations:
    parseFloat("2.5") will be 2.5
    parseFloat(" 2.5") will be 2.5
    parseFloat("2.5garbage") will be 2.5
    parseFloat("2.5garbage3.2") will be 2.5
    it will only be NaN if the value of the string starts with a
    non-
    numerical character
    parseFloat("garbage2.5") will be NaN
    So doing something like that, will not work for me:
    public function isFloat : Boolean(str : String) {
    if (isNaN(parseFloat(str))) {
    return false;
    return true;
    I want to check if the string is a *real* float that is:
    isFloat("2.5") will be true
    isFloat("x2.5") will be false
    isFloat("2.5x3.2") will be false
    isFloat("2.5x") will be false
    Basically, no spaces or non-numerical characters should be
    allowed
    (expect the one dot '.' if required).
    I'm thinking of implementing my own isFloat method, checking
    character by
    character to see if the value is really a float or not.
    Something like
    that (quick draft, haven't tested it or compiled it):
    public function isFloat : Boolean (str : String) {
    var dotUsed : Boolean = false;
    for (var i : int = 0; i < str.length; ++i) {
    if (str.charAt(i) == '.') {
    if (!dotUsed) {
    dotUsed = true;
    } else {
    return false;
    } else if (isNaN(parseInt(str.charAt(i)))) {
    return false;
    However, I'm not happy with the runtime efficiency here. I'm
    sure there's
    some more efficient way to detect that a string is a float;
    or another
    simple method I haven't look at.
    Any thoughts?
    Thanks

    You can avoid the looping test by using a regular expression.
    Basically the whole check could be done with a regular expression.
    I'd love to provide the code, but regular expressions are new to me
    as well. I had a play around with them and got close, but it didn't
    pass all the tests, so I wasn't doing something right. Someone else
    will post an example I'm sure.

  • Convert string to float

    It seems extremely rudimentary but I haven't been able to find an answer yet.
    I would like to pass in a string representing a human-readable floating point (ie, non IEEE 754) and get its value.
    A straight up assignment of a string variable into a f variable doesn't work for thousand dividers. We would also need it for every scenarios:
    123.456
    123,456
    123456
    123456,789
    123.456,789
    123,456.789
    all should be valid inputs and the resolution should be dependent on system setting for number formats.
    I would like a built-in ABAP call with no manual processing. I'm sure this problem has been encountered thousands of times and solved thousands of times. No point reinventing the wheel.

    Tested Code
    Output as below
    String : 123,456,789,123.456
    String : 123456789123.456
    Float  :   1.2345678912345599E+11
    DATA v_str TYPE STRING VALUE '123,456,789,123.456'.
    DATA v_flt TYPE F.
    WRITE: / 'String :', v_str. "With Commas
    REPLACE ALL OCCURRENCES OF ',' IN v_str WITH ''.
    WRITE: / 'String :', v_str. "Without Commas
    CATCH SYSTEM-EXCEPTIONS ARITHMETIC_ERRORS = 1
                            CONVERSION_ERRORS = 2.
      MOVE v_str TO v_flt.
    ENDCATCH.
    WRITE: / 'Float  :', v_flt. "Float value

  • Problem in Converting string to float

    Hi,
    I am reading from textField and trying to convert that string value to float.
    Compiler is giving error cannot find symbol toFloat even though I have included java.lang in my program.
    Please help me with this one.
    Thanks.

    Use
    float f = Float.parseFloat(inputString); //where inputString is the string you want to parse

Maybe you are looking for

  • Can't send animated .gif files

    Hi - hope you can help. I have tried everything! I receive wonderful moving animations via email but when I go to forward them either to myself (to check) or to others, they are received as still pics, e.g. someone sent me a picture of a guy throwing

  • HT1926 iTunes installation/run failure due to C runtime library access failure

    iTunes update 11.1.4.62 fails when installing/running in Windows 7. System - see C++ Runtime Library error popup, below. I have tried uninstalling iTunes, rebooting, and re-installing with the same results. Any ideas?

  • Get exData body of a static text

    Hi experts, I've got a static text as follow (xml source)         <draw minH="20mm" name="Text" w="190mm" x="0.6477mm" y="201.971mm">             <ui>                <textEdit>                   <margin/>                </textEdit>             </ui>

  • Default settings ( mail, contacts, calendars) on the iPhone 5?

    Can someone please tell me the defaulty settings are? My little cousin was playing around with it and messed everything up and I want it back to normal. Thanks!

  • Thinkpad Yoga: Lenovo Power Plans

    Hello i upgraded my Yoga to Windows 8.1. Enterprise but no i lost my lenovo energy plans. Can you tell me how to get them like energysaver again? Can anyone export them like here http://www.intowindows.com/how-to-import-and-export-power-plans-schemes