Convert float to integer

how to convert float to integer?
and convert it back from integer to float?

You can cast a float to an integer using the following syntax:
float f = 1.245;
int i = (int)f;
To convert an int to a float you use a similar syntax:
int i = 3;
float f = (float)i;
You'll also find some interesting methods for the Float and Integer object s in the Java API documentation.

Similar Messages

  • Convert float to 4 bytes array

    How can I convert float type to 4 byte array -
    not with strings but to exact binary representation.

    See the javadoc of Float.floatToIntBits. Converting an int to an array of 4 bytes is easy, so I will left it as an exercise.
    floatToIntBits
    public static int floatToIntBits(float value)Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.
    Bit 31 (the bit that is selected by the mask 0x80000000) represents the sign of the floating-point number. Bits 30-23 (the bits that are selected by the mask 0x7f800000) represent the exponent. Bits 22-0 (the bits that are selected by the mask 0x007fffff) represent the significand (sometimes called the mantissa) of the floating-point number.
    If the argument is positive infinity, the result is 0x7f800000.
    If the argument is negative infinity, the result is 0xff800000.
    If the argument is NaN, the result is 0x7fc00000.
    In all cases, the result is an integer that, when given to the intBitsToFloat(int) method, will produce a floating-point value the same as the argument to floatToIntBits (except all NaN values are collapsed to a single "canonical" NaN value).
    Parameters:
    value - a floating-point number.
    Returns:
    the bits that represent the floating-point number.

  • Function module to convert float value to data type 'dec'

    Hi experts,
      In a report i need to convert float value to the data type 'DEC'. How to convert it. Is there any function module for this conversion.
    Thanks and Regards,
    Vaibhav Tiwari.

    Hi ..
    We can do like this...
    Data : V_float type F value '12345.67'.
    Data: V_dec type P Decimals 2.
    Write:/ V_float exponent 0. "This will display it like Type P
    or
    Write V_float to V_dec EXPONENT 0.
    Write:/ V_dec.
    reward if Helpful.
    <b></b>

  • Convert float to rational

    Hello,
    The TIFF tag header protocol requires a data type I am not really conversant with, the Rational Data type: http://en.wikipedia.org/wiki/Rational_data_type
    I need to convert double precision numbers to this data type, e.g. 24.5 needs to be a ratio of two integers: {49,2}. Is there an existing LabVIEW algorithm to perform this operation? All my NI searches keep resulting in Rational ClearCase or Rational Array interpolation. I seem to have found a lot of the theory behind this: http://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions
    But, is this implemented in labVIEW already, or should I implement the above-referenced c-code?
    Thanks,
    Mello
    Data Science Automation
    CTA, CLA, CPI
    SHAZAM!
    Solved!
    Go to Solution.

    I was curious if there were other ways to do this and found that Scilab combined with the LabVIEW Gateway works quite well. The 'rat' function is available in Scilab; Scilab and the LabVIEW gateway were both easy to install...
    http://www.scilab.org/products/other/labview_gateway
    Message Edited by Phillip Brooks on 03-04-2010 08:29 AM
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness
    Attachments:
    scilab_rat.JPG ‏46 KB

  • Converting float array to Float array

    Hi, is there any efficent way to convert float[][] to Float[][] without iterating the array and creating Float object for each float data? I have a method performing calculations based on a 2D array( float[][]) and then need to store the result in a TreeMap data structure. The "put" method for TreeMap ask Object type.
    Thanks in advance!

    1) No
    2) An array of float is an object as well:
    Object obj = new float[]{1.0, 2.0, 3.0};3) What you want to store? If your structure is somewhat like this:
    float[][] f = {
       { 1.0, 2.0, 3.0 },
       { 4.0, 5.0, 6.0 },
    };and you want to "index" your TreeMap by the first element of each row of your float[] array, you could do something like this:
        SortedMap sm = new TreeMap();
        sm.put(new Float(f[0][0]), f[0]); // Arrays are objects as well
        sm.put(new Float(f[1][0]), f[1]); //and so on.

  • Arithmetic Overflow error converting float to data type numeric

    Hi,
    Am facing strange issue,I have function which returns money datatype and assigning the return money value to float datatype in table.
    Error msg:
    Msg 8115, Level 16, State 6, Procedure GBCalcCatalogPriceNewV2, Line 204
    Arithmetic overflow error converting float to data type numeric.
    The statement has been terminated.
    Strange thing is the same stored procedure is working fine in production environment,but in the deveopment i see this error.Am scared if the same happens in the production environment.Please advice and advance
    thanks
    Regards
    RAj

    Strange thing is the same stored procedure is working fine in production environment,
    How could that be strange? This is an error that occurs depending on the data. Accidents that are waiting to happen will happen sooner or later.
    Then again, a development database may be more prone to such errors, because data that entered are completely out of whack with real life data. Still it is a warning sign. If you have some place where you convert data from float to numeric, you must consider
    the risk that the float value is outside the range for the numeric data type. How do you prevent that from happening? Maybe a CHECK constraint on the column? Of if the data origins from a money column, use a numeric data type with sufficient precision.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Convert float to Float

    Hi,
    How can I convert float to Float?
    thanks
    andrew

    I got the answer myself, Float Q = new Float(q);

  • Convert hexa to integer

    Hello,
    I have a flat file in which I get a value in hexadecimal format.
    Can we convert it in integer format in ODI ? I did'nt find anything about that.
    Thanks in advance
    Marie

    What is your target technology ?
    There are ways to do it. One of the way is to create function
    You can create ODI function to convert Hexadecimal to Integer.
    For example if you are using Oracle as your staging or target technology .. you can use
    Oracle you can use
    SYS_OP_RAWTONUM('AA') or to_number('AA', 'xx').
    Other can be directly using features of target or staging technologies to get the desired output.

  • Converting java's integer variable into mysql's tinyint datatype

    I am using mysql database and to store the values of the table I used tinyint as datatype in the mysql. I have to convert java's integer data type to mysql's tinyint data type. how it is possible. please help me out.
    Thanks ...

    Do you have to use int?
    The range of tinyint is -128 to 127, which matches the primitive byte data type in Java exactly. Maybe this primitive would be more suited to your needs? (sorry I have no advice on your actual question :p)
    Illu

  • I would like to convert a float to integer..........

    First of all, avoid my previous question.
    Secondly, i would like to have an output in my program float numbers but until one decimal . for example 23.5 56.8 23.8
    How can I do that.I know there is a rounding method but a number like 23.546935609 it will be 24. I do not want that.
    For understanding my program ,it is read a number as a command argument ,so string, i converted as a float for taking : 23,7 but i do not want to take from the command line 23,98765544........it is too big for my job.
    Any suggestions?Can i use try catch commands for this?

    Also, if you'd just like to format the output, you can use the printf() method.
    i.e.
    public class Test {
         public static void main(String[] argv) {
              float g = 1.234567890F;
              System.out.printf("To one decimal place: %.1f\n", g);
              System.out.printf("To two decimal places: %.2f\n", g);
    }

  • Error trying to convert float to expanded form for a check application

    I am writing a program to generate a check, but when it tries to parse the Float (netpay) into a String in expanded form (e.g. 1.23 becomes 1 dollar and twenty-three cents) it throws weird arrayOutOfBounds exceptions
    * Check.java
    * Created on January 23, 2008, 9:55 AM
    package payrollapplication;
    public class Check extends javax.swing.JFrame {
    public Employee emp;
    String expanded;
    /** Creates new form Check */
    boolean debug = true;
    public Check() {
    initComponents();
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
    date = new javax.swing.JLabel();
    jPanel1 = new javax.swing.JPanel();
    name = new javax.swing.JLabel();
    amtexpanded = new javax.swing.JLabel();
    amtnum = new javax.swing.JLabel();
    empid = new javax.swing.JLabel();
    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("EMP Name Here");
    setAlwaysOnTop(true);
    setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowActivated(java.awt.event.WindowEvent evt) {
    formWindowActivated(evt);
    date.setText("DATE");
    jPanel1.setBackground(new java.awt.Color(102, 102, 0));
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 429, Short.MAX_VALUE)
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 62, Short.MAX_VALUE)
    name.setText("NAME");
    amtexpanded.setText("AMT EXPANDED");
    amtnum.setText("AMT #");
    empid.setText("EMPID");
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(10, 10, 10)
    .addComponent(amtexpanded, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE))
    .addComponent(name, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(amtnum, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(date, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
    .addComponent(empid, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
    .addContainerGap())
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(14, 14, 14)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(date)
    .addComponent(name))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(amtnum)
    .addComponent(amtexpanded, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(empid)
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    pack();
    }// </editor-fold>
    private void formWindowActivated(java.awt.event.WindowEvent evt) {                                    
    initDisplay(); //Computes the expanded form of the number and fills the windows with information
    amtexpanded.setText(expanded);
    * @param args the command line arguments
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Check().setVisible(true);
    public void initDisplay() {
    try {
    amtnum.setText("$"+emp.netpay); //Fill in the net pay
    name.setText(""+emp.fname+" "+emp.mname+" "+emp.lname+" "+emp.suffix); //Fill in the name
    System.out.println("NOT YET IMPLEMENTED");
    expanded = "";
    //begin to compute the expanded form
    //determine the length of the left side of the decimal
    expanded = emp.netpay+"";
    String[] split = expanded.split(".");
    int length = split[0].length();
    //Now we compute the first half of the number
    int wholeLength = split[0].length();
    char[] arr = split[0].toCharArray(); //create the character array
    expanded = ""; //Clear it
    if(length == 3) {
    System.out.println("Length of 3, working...");
    //Assume we are starting at the one thousands
    //Since 0 is 1, we need to test for 3 for 1,000s not 4
    switch(arr[3]) {
    case '1': expanded = "One Thousand ";
    case '2': expanded = "Two Thousand ";
    case '3': expanded = "Three Thousand ";
    case '4': expanded = "Four Thousand ";
    case '5': expanded = "Five Thousand ";
    case '6': expanded = "Six Thousand ";
    case '7': expanded = "Seven Thousand ";
    case '8': expanded = "Eight Thousand ";
    case '9': expanded = "Nine Thousand ";
    switch(arr[2]) {
    case '1': expanded = expanded + "One-Hundred ";
    case '2': expanded = expanded + "Two-Hundred ";
    case '3': expanded = expanded + "Three-Hundred ";
    case '4': expanded = expanded + "Four-Hundred ";
    case '5': expanded = expanded + "Five-Hundred ";
    case '6': expanded = expanded + "Six-Hundred ";
    case '7': expanded = expanded + "Seven-Hundred ";
    case '8': expanded = expanded + "Eight-Hundred ";
    case '9': expanded = expanded + "Nine-Hundred ";
    switch(arr[1]) {
    case '0': {
    switch(arr[0]) {
    case '0': expanded = expanded + "";
    case '1': expanded = expanded + "and One";
    case '2': expanded = expanded + "and Two";
    case '3': expanded = expanded + "and Three";
    case '4': expanded = expanded + "and Four";
    case '5': expanded = expanded + "and Five";
    case '6': expanded = expanded + "and Six";
    case '7': expanded = expanded + "and Seven";
    case '8': expanded = expanded + "and Eight";
    case '9': expanded = expanded + "and Nine";
    case '1': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Ten";
    case '1': expanded = expanded + "and Eleven";
    case '2': expanded = expanded + "and Twelve";
    case '3': expanded = expanded + "and Thirteen";
    case '4': expanded = expanded + "and Fourteen";
    case '5': expanded = expanded + "and Fifteen";
    case '6': expanded = expanded + "and Sixteen";
    case '7': expanded = expanded + "and Seventeen";
    case '8': expanded = expanded + "and Eighteen";
    case '9': expanded = expanded + "and Nineteen";
    case '2': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Twenty";
    case '1': expanded = expanded + "and Twenty-One";
    case '2': expanded = expanded + "and Twenty-Two";
    case '3': expanded = expanded + "and Twenty-Three";
    case '4': expanded = expanded + "and Twenty-Four";
    case '5': expanded = expanded + "and Twenty-Five";
    case '6': expanded = expanded + "and Twenty-Six";
    case '7': expanded = expanded + "and Twenty-Seven";
    case '8': expanded = expanded + "and Twenty-Eight";
    case '9': expanded = expanded + "and Twenty-Nine";
    case'3': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Thirty";
    case '1': expanded = expanded + "and Thirty-One";
    case '2': expanded = expanded + "and Thirty-Two";
    case '3': expanded = expanded + "and Thirty-Three";
    case '4': expanded = expanded + "and Thirty-Four";
    case '5': expanded = expanded + "and Thirty-Five";
    case '6': expanded = expanded + "and Thirty-Six";
    case '7': expanded = expanded + "and Thirty-Seven";
    case '8': expanded = expanded + "and Thirty-Eight";
    case '9': expanded = expanded + "and Thirty-Nine";
    case '4': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Fourty";
    case '1': expanded = expanded + "and Fourty-One";
    case '2': expanded = expanded + "and Fourty-Two";
    case '3': expanded = expanded + "and Fourty-Three";
    case '4': expanded = expanded + "and Fourty-Four";
    case '5': expanded = expanded + "and Fourty-Five";
    case '6': expanded = expanded + "and Fourty-Six";
    case '7': expanded = expanded + "and Fourty-Seven";
    case '8': expanded = expanded + "and Fourty-Eight";
    case '9': expanded = expanded + "and Fourty-Nine";
    case '5': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Fifty";
    case '1': expanded = expanded + "and Fifty-One";
    case '2': expanded = expanded + "and Fifty-Two";
    case '3': expanded = expanded + "and Fifty-Three";
    case '4': expanded = expanded + "and Fifty-Four";
    case '5': expanded = expanded + "and Fifty-Five";
    case '6': expanded = expanded + "and Fifty-Six";
    case '7': expanded = expanded + "and Fifty-Seven";
    case '8': expanded = expanded + "and Fifty-Eight";
    case '9': expanded = expanded + "and Fifty-Nine";
    case '6': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Sixty";
    case '1': expanded = expanded + "and Sixty-One";
    case '2': expanded = expanded + "and Sixty-Two";
    case '3': expanded = expanded + "and Sixty-Three";
    case '4': expanded = expanded + "and Sixty-Four";
    case '5': expanded = expanded + "and Sixty-Five";
    case '6': expanded = expanded + "and Sixty-Six";
    case '7': expanded = expanded + "and Sixty-Seven";
    case '8': expanded = expanded + "and Sixty-Eight";
    case '9': expanded = expanded + "and Sixty-Nine";
    case '7': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Seventy";
    case '1': expanded = expanded + "and Seventy-One";
    case '2': expanded = expanded + "and Seventy-Two";
    case '3': expanded = expanded + "and Seventy-Three";
    case '4': expanded = expanded + "and Seventy-Four";
    case '5': expanded = expanded + "and Seventy-Five";
    case '6': expanded = expanded + "and Seventy-Six";
    case '7': expanded = expanded + "and Seventy-Seven";
    case '8': expanded = expanded + "and Seventy-Eight";
    case '9': expanded = expanded + "and Seventy-Nine";
    case '8': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Eighty";
    case '1': expanded = expanded + "and Eighty-One";
    case '2': expanded = expanded + "and Eighty-Two";
    case '3': expanded = expanded + "and Eighty-Three";
    case '4': expanded = expanded + "and Eighty-Four";
    case '5': expanded = expanded + "and Eighty-Five";
    case '6': expanded = expanded + "and Eighty-Six";
    case '7': expanded = expanded + "and Eigthy-Seven";
    case '8': expanded = expanded + "and Eighty-Eight";
    case '9': expanded = expanded + "and Eighty-Nine";
    case '9': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Ninety";
    case '1': expanded = expanded + "and Ninety-One";
    case '2': expanded = expanded + "and Ninety-Two";
    case '3': expanded = expanded + "and Ninety-Three";
    case '4': expanded = expanded + "and Ninety-Four";
    case '5': expanded = expanded + "and Ninety-Five";
    case '6': expanded = expanded + "and Ninety-Six";
    case '7': expanded = expanded + "and Ninety-Seven";
    case '8': expanded = expanded + "and Ninety-Eight";
    case '9': expanded = expanded + "and Ninety-Nine";
    //Now we move down the length, 2, 1 ,
    if(length == 2) {
    System.out.println("length of 2, working...");
    switch(arr[2]) {
    case '1': expanded = expanded + "One-Hundred ";
    case '2': expanded = expanded + "Two-Hundred ";
    case '3': expanded = expanded + "Three-Hundred ";
    case '4': expanded = expanded + "Four-Hundred ";
    case '5': expanded = expanded + "Five-Hundred ";
    case '6': expanded = expanded + "Six-Hundred ";
    case '7': expanded = expanded + "Seven-Hundred ";
    case '8': expanded = expanded + "Eight-Hundred ";
    case '9': expanded = expanded + "Nine-Hundred ";
    switch(arr[1]) {
    case '0': {
    switch(arr[0]) {
    case '0': expanded = expanded + "";
    case '1': expanded = expanded + "and One";
    case '2': expanded = expanded + "and Two";
    case '3': expanded = expanded + "and Three";
    case '4': expanded = expanded + "and Four";
    case '5': expanded = expanded + "and Five";
    case '6': expanded = expanded + "and Six";
    case '7': expanded = expanded + "and Seven";
    case '8': expanded = expanded + "and Eight";
    case '9': expanded = expanded + "and Nine";
    case '1': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Ten";
    case '1': expanded = expanded + "and Eleven";
    case '2': expanded = expanded + "and Twelve";
    case '3': expanded = expanded + "and Thirteen";
    case '4': expanded = expanded + "and Fourteen";
    case '5': expanded = expanded + "and Fifteen";
    case '6': expanded = expanded + "and Sixteen";
    case '7': expanded = expanded + "and Seventeen";
    case '8': expanded = expanded + "and Eighteen";
    case '9': expanded = expanded + "and Nineteen";
    case '2': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Twenty";
    case '1': expanded = expanded + "and Twenty-One";
    case '2': expanded = expanded + "and Twenty-Two";
    case '3': expanded = expanded + "and Twenty-Three";
    case '4': expanded = expanded + "and Twenty-Four";
    case '5': expanded = expanded + "and Twenty-Five";
    case '6': expanded = expanded + "and Twenty-Six";
    case '7': expanded = expanded + "and Twenty-Seven";
    case '8': expanded = expanded + "and Twenty-Eight";
    case '9': expanded = expanded + "and Twenty-Nine";
    case'3': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Thirty";
    case '1': expanded = expanded + "and Thirty-One";
    case '2': expanded = expanded + "and Thirty-Two";
    case '3': expanded = expanded + "and Thirty-Three";
    case '4': expanded = expanded + "and Thirty-Four";
    case '5': expanded = expanded + "and Thirty-Five";
    case '6': expanded = expanded + "and Thirty-Six";
    case '7': expanded = expanded + "and Thirty-Seven";
    case '8': expanded = expanded + "and Thirty-Eight";
    case '9': expanded = expanded + "and Thirty-Nine";
    case '4': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Fourty";
    case '1': expanded = expanded + "and Fourty-One";
    case '2': expanded = expanded + "and Fourty-Two";
    case '3': expanded = expanded + "and Fourty-Three";
    case '4': expanded = expanded + "and Fourty-Four";
    case '5': expanded = expanded + "and Fourty-Five";
    case '6': expanded = expanded + "and Fourty-Six";
    case '7': expanded = expanded + "and Fourty-Seven";
    case '8': expanded = expanded + "and Fourty-Eight";
    case '9': expanded = expanded + "and Fourty-Nine";
    case '5': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Fifty";
    case '1': expanded = expanded + "and Fifty-One";
    case '2': expanded = expanded + "and Fifty-Two";
    case '3': expanded = expanded + "and Fifty-Three";
    case '4': expanded = expanded + "and Fifty-Four";
    case '5': expanded = expanded + "and Fifty-Five";
    case '6': expanded = expanded + "and Fifty-Six";
    case '7': expanded = expanded + "and Fifty-Seven";
    case '8': expanded = expanded + "and Fifty-Eight";
    case '9': expanded = expanded + "and Fifty-Nine";
    case '6': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Sixty";
    case '1': expanded = expanded + "and Sixty-One";
    case '2': expanded = expanded + "and Sixty-Two";
    case '3': expanded = expanded + "and Sixty-Three";
    case '4': expanded = expanded + "and Sixty-Four";
    case '5': expanded = expanded + "and Sixty-Five";
    case '6': expanded = expanded + "and Sixty-Six";
    case '7': expanded = expanded + "and Sixty-Seven";
    case '8': expanded = expanded + "and Sixty-Eight";
    case '9': expanded = expanded + "and Sixty-Nine";
    case '7': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Seventy";
    case '1': expanded = expanded + "and Seventy-One";
    case '2': expanded = expanded + "and Seventy-Two";
    case '3': expanded = expanded + "and Seventy-Three";
    case '4': expanded = expanded + "and Seventy-Four";
    case '5': expanded = expanded + "and Seventy-Five";
    case '6': expanded = expanded + "and Seventy-Six";
    case '7': expanded = expanded + "and Seventy-Seven";
    case '8': expanded = expanded + "and Seventy-Eight";
    case '9': expanded = expanded + "and Seventy-Nine";
    case '8': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Eighty";
    case '1': expanded = expanded + "and Eighty-One";
    case '2': expanded = expanded + "and Eighty-Two";
    case '3': expanded = expanded + "and Eighty-Three";
    case '4': expanded = expanded + "and Eighty-Four";
    case '5': expanded = expanded + "and Eighty-Five";
    case '6': expanded = expanded + "and Eighty-Six";
    case '7': expanded = expanded + "and Eigthy-Seven";
    case '8': expanded = expanded + "and Eighty-Eight";
    case '9': expanded = expanded + "and Eighty-Nine";
    case '9': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Ninety";
    case '1': expanded = expanded + "and Ninety-One";
    case '2': expanded = expanded + "and Ninety-Two";
    case '3': expanded = expanded + "and Ninety-Three";
    case '4': expanded = expanded + "and Ninety-Four";
    case '5': expanded = expanded + "and Ninety-Five";
    case '6': expanded = expanded + "and Ninety-Six";
    case '7': expanded = expanded + "and Ninety-Seven";
    case '8': expanded = expanded + "and Ninety-Eight";
    case '9': expanded = expanded + "and Ninety-Nine";
    if(length == 1) {
    System.out.println("Length of 1 working....");
    switch(arr[1]) {
    case '0': {
    switch(arr[0]) {
    case '0': expanded = expanded + "";
    case '1': expanded = expanded + "and One";
    case '2': expanded = expanded + "and Two";
    case '3': expanded = expanded + "and Three";
    case '4': expanded = expanded + "and Four";
    case '5': expanded = expanded + "and Five";
    case '6': expanded = expanded + "and Six";
    case '7': expanded = expanded + "and Seven";
    case '8': expanded = expanded + "and Eight";
    case '9': expanded = expanded + "and Nine";
    case '1': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Ten";
    case '1': expanded = expanded + "and Eleven";
    case '2': expanded = expanded + "and Twelve";
    case '3': expanded = expanded + "and Thirteen";
    case '4': expanded = expanded + "and Fourteen";
    case '5': expanded = expanded + "and Fifteen";
    case '6': expanded = expanded + "and Sixteen";
    case '7': expanded = expanded + "and Seventeen";
    case '8': expanded = expanded + "and Eighteen";
    case '9': expanded = expanded + "and Nineteen";
    case '2': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Twenty";
    case '1': expanded = expanded + "and Twenty-One";
    case '2': expanded = expanded + "and Twenty-Two";
    case '3': expanded = expanded + "and Twenty-Three";
    case '4': expanded = expanded + "and Twenty-Four";
    case '5': expanded = expanded + "and Twenty-Five";
    case '6': expanded = expanded + "and Twenty-Six";
    case '7': expanded = expanded + "and Twenty-Seven";
    case '8': expanded = expanded + "and Twenty-Eight";
    case '9': expanded = expanded + "and Twenty-Nine";
    case'3': {
    switch(arr[0]) {
    case '0': expanded = expanded + "and Thirty";
    case '1': exp

    if(length == 3) {
    System.out.println("Length of 3, working...");
    //Assume we are starting at the one thousands
    //Since 0 is 1, we need to test for 3 for 1,000s not 4
    switch(arr[3]) {Arrays in Java start at index zero, so if the length of an array is 3, then the valid indices are 0,1 and 2.
    You also can make the whole thing much cleaner with using the way things are phrased in English - you say "one million, one hundred and fifty thousand, two hundred and seventy six pounds, thirty seven pence", so if you write a routine which converts an integer between 0 (inclusive) and 1000 (exclusive), and call that for the millions, thousands, units and cents in turn. You also might want to use localized strings in an array instead of coding everything as a sequence of switch statements.

  • Convert string to integer

    package onjava;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import org.apache.soap.*;
    import org.apache.soap.rpc.*;
    import java.lang.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class CalcClient extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
    out.println("\"http://www.w3.org/TR/html4/loose.dtd\">");
    out.println("<html>");
    out.println("<head>");
    out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
    out.println("<title>Substraction using SOAP</title>");
    out.println("</head>");
        URL url = new URL ("http://localhost/soap/servlet/rpcrouter");
    Integer p1=request.getParameter("param1");
    Integer p2=request.getParameter("param2");
    In the above statement i have to convert the string to integer because that has to be passed in my program as an argument to a function so please let me know how to do that
        // Build the call.
        Call call = new Call();
        call.setTargetObjectURI("urn:onjavaserver");
        call.setMethodName("subtract");
        call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
        Vector params = new Vector();
        params.addElement(new Parameter("p1", Integer.class, p1, null));
        params.addElement(new Parameter("p2", Integer.class, p2, null));
        call.setParams (params);
        // make the call: note that the action URI is empty because the
        // XML-SOAP rpc router does not need this. This may change in the
        // future.
        Response resp = call.invoke(url, "" );
        // Check the response.
        if ( resp.generatedFault() ) {
          Fault fault = resp.getFault ();
         out.println("The call failed: ");
         out.println("Fault Code   = " + fault.getFaultCode());
         out.println("Fault String = " + fault.getFaultString());
        else {
          Parameter result = resp.getReturnValue();
          out.println(result.getValue());
    out.println("</body>");
    out.println("</html>");

    Two possibilities: Try either java.lang.Integer.valueOf() or java.text.NumberFormat and its parse method.
    Either one will do what you want. I think Integer will be the simpler of the two.
    The code you have is obviously not correct, because getParameter returns a String:
    Integer p1=request.getParameter("param1");Do it like this:
    Integer p1=Integer.valueOf(request.getParameter("param1"));%

  • Converting String to integer?

    I looked on the sun forums and only found a lot of things that convert integers to Strings. Nothing to do the opposite.
    Does anyone know how I can convert a String to an int?

    coopkev2 wrote:
    an "A" from a "B"?? I don't understand what you are saying... I want to convert "100" to 100.
    Edit-- I'll be back soon.Read "A" as "Integer" and "B" as "String".
    In this case: read the [java.lang.Integer API|http://java.sun.com/javase/6/docs/api/java/lang/Integer.html] if there is a method which takes a String and returns an Integer or int.

  • Convert Float to Char

    Hi,
    I'm having problem when converting a float variable to char - it seems like the float value is automatically rounded into the char variable, but I don't want that.
    Does anybody know a wafunction or a way of avoiding this convertion round?
    Thanks,
    Nuno Afonso

    With the input parameters you gave me...
    Error in ASSIGN: Memory protection error
    The current program "SAPLSCFL " uses the ASSIGN statement. 
    However, the offset of the segment (536) plus the length (1)
    is greater than the length of the segment (536).           
    This is not allowed.                                       

  • Converting float to binary

    Hi,
    I was wondering if anyone knew of a way to convert a float value (as ascii) to a binary value?
    I need a program that will take in a number of floating values from a file, convert the values to binary and then write out these values in another file, 4 bytes at a time.
    I know that you can convert decimals to binary with an easy function but i've searched for float to binary and came up with nothing.
    any help would be really appreciated,
    cheers!

    One of the Java's problems is that you can easily get lost in the sea of Javadocs...
    java.io.DataOutputStream
    writeFloat(float v)
    Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.
    java.io.DataInputStream
    readFloat
    public float readFloat()
    throws IOException
    Reads four input bytes and returns a float value. It does this by first constructing an int value in exactly the manner of the readInt method, then converting this int value to a float in exactly the manner of the method Float.intBitsToFloat. This method is suitable for reading bytes written by the writeFloat method of interface DataOutput.

Maybe you are looking for