Controlling precision in java float variable

Hi,
I need a small clarification. In java, is there any way to control precision of float values.
For ex, when I perform the below
float f1 = 0.0F;
f1 = 200/11;
The result will be = 18.181818
But i want only *18.18*. Is there any way to perform this truncation? Please help me on this.
Thanks

There is no way to control the precision except on printout where you can use java.text.DecimalFormat or String.format() or java.util.Formatter.
P.S. The result of
f1 = 200/11;
will be 18 not 18.181818... because the division will be done as integers.
Edited by: sabre150 on Dec 24, 2007 8:44 AM

Similar Messages

  • Round float variables

    hi folks: i need to round the values in my float variables to 2 points precision like this x.xx, so that i can compare them easily.
    for example: 0.08765432 and 0.09087654 would be esaier for me to compare if i could round them to: 0.09 and 0.09(equal). otherwise those numbers will, virtually, never equal ech others.
    is ther a way to round up my float variables to x.xx precision?
    thx

    Rather than comparing string representations of integers, I'd suggest just using a delta:
    double delta = 0.0000005;
    double someNumber1, someNumber2;
    // get values for someNumber1, someNumber2
    if (Math.abs(someNumber1 - someNumber2) < delta) {
        // say they're equal
    }But actually using BigDecimal is probably the better choice.

  • How to access a JAVA Script variable in JSP Code

    How to access a JAVA Script variable in JSP Code. I have been unable todo this.
    Plz Suggest a way.
    Thanks
    Soumya

    try to do this code
    String s=request.getParameter("javascriptvariablename");

  • Returning/passing Java script variable/value to PL/SQL environment

    Hi,
    Can someone give me a sample code about how to return a Java script variable to PLSQL environment?
    for example, I have a javascript function that returns timestamp in milisecond, I then want to write PLSQL code to call this java script function and save its value in a number-typed plsql variable for further calculation, I dont know how the two scripting language communicate.
    Thank you very much
    Binh
    null

    This is quite simple.
    I assume that you are able to define html form side through pl/sql. Suppose your new pl/sql value is v_time then in the procedure that you write include one more statement
    htp.p('<Input type=hidden name="v_time" value="">');
    and also in the form invocation,
    htp.p('<form name=app method=post action="time_handler" onSubmit="return false;">');
    and to the end you must be having a submit
    to it attach a onClick function that calls
    simply our validate function..
    eg htp.p('<Input type=submit value="process"
    onClick="validate();">');
    Include in the head portion
    the following script using htp.p procedure
    function validate()
    app.v_time.value=<calculated value>;
    return app.submit();
    Hope this helps.
    Nat
    null

  • [AwesomeWM] mouse cursor precision in java apps

    My mouse cursor isn't really precise in java apps, for example IntelliJ IDEA. The highlighted (and clicked) item is always below the cursor.
    In this case https://docs.google.com/file/d/0B594FZm … sp=sharing (Screenshot) the mouse cursor actually hightlights the row above, but the row below is selected and clicked).
    Last edited by Leandros (2013-04-12 11:59:43)

    I am having the same issue with RubyMine 6 and Oracle JDK 1.7.0_45. Apparently, we are not the only ones. http://unix.stackexchange.com/questions … 454afd0e69 However I have not been able to find any solutions yet.

  • How 2 extract 2 decimal places from a float variable

    Hiya,
    I have a float variable say x with a value 10.56879, how do i extract 56 without rounding it up. I want to store 56 in an integer variable after extracting it from float.
    Cheers
    Deepak

    This works when f is positive,
    float f = 10.56879f;
    int i = Math.round((float)Math.floor(((f - (float)Math.floor(f))*100.0f)));
    System.out.println(i);

  • JAVA ENVIRONMENTAL VARIABLE AND PATH SETTINGS

    Hi all,
    I am having a pecuilar problem with java environment variables and setting the path.
    I am trying to install and check the instalation using some pre-provided batch commands from Open GTS.
    The error log is as below .
    ** Found 2 Error(s)!
    1) The 'PATH' environment variable points to the JRE, rather than the JDK.
       [Reason: The 'PATH' environment variable points to the JRE (Java Runtime
         Environment), rather than the JDK (Java Developer Kit).  The JDK already
         contains the JRE, so a separate JRE insallation  is not necessary.]
       [Fix: Set the 'PATH' environment variable to point to the JDK installation bi
    n
         directory.]
    2) 'JAVA_HOME' does not match the Java installation 'PATH' directory 'C:\Program
    Files\Java\jre6'.
       [Reason: The version of Java referenced in the executable 'PATH' environment
         variable does not match 'JAVA_HOME'.]
       [Fix: Make sure both the 'JAVA_HOME' and 'PATH' environment variables point t
    o the
         same installed JDK.]
    No warnings reported
    Further more these may help understand better
    C:\Dush\Workspaces\JavaWorkspace\OpenGTS>echo %JAVA_HOME%
    C:\Program Files\Java\jdk1.6.0_03
    C:\Dush\Workspaces\JavaWorkspace\OpenGTS>path
    PATH=C:\Program Files\PC Connectivity Solution\;C:\WINDOWS\system32;C:\WINDOWS;C
    :\WINDOWS\System32\Wbem;C:\Program Files\apache-ant-1.7.0\bin;C:\Program Files\J
    ava\jdk1.6.0_03\bin;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Dush\Workspac
    es\JavaWorkspace\OpenGTS\bin;
    C:\Dush\Workspaces\JavaWorkspace\OpenGTS>The resolution suggests that the path point to the same instance of jdk installation but that has already been done,
    I do not understand from where does the
    C:\Program Files\Java\jre6 path get set.
    Please suggest..

    dushdushyant wrote:
    ** Found 2 Error(s)!
    1) The 'PATH' environment variable points to the JRE, rather than the JDK.
    [Reason: The 'PATH' environment variable points to the JRE (Java Runtime
    Environment), rather than the JDK (Java Developer Kit).  The JDK already
    contains the JRE, so a separate JRE insallation  is not necessary.]
    [Fix: Set the 'PATH' environment variable to point to the JDK installation bi
    n
    directory.]
    2) 'JAVA_HOME' does not match the Java installation 'PATH' directory 'C:\Program
    Files\Java\jre6'.
    [Reason: The version of Java referenced in the executable 'PATH' environment
    variable does not match 'JAVA_HOME'.]
    [Fix: Make sure both the 'JAVA_HOME' and 'PATH' environment variables point t
    o the
    same installed JDK.]
    No warnings reported
    C:\Dush\Workspaces\JavaWorkspace\OpenGTS>echo %JAVA_HOME%
    C:\Program Files\Java\jdk1.6.0_03
    C:\Dush\Workspaces\JavaWorkspace\OpenGTS>path
    PATH=C:\Program Files\PC Connectivity Solution\;C:\WINDOWS\system32;C:\WINDOWS;C
    :\WINDOWS\System32\Wbem;C:\Program Files\apache-ant-1.7.0\bin;C:\Program Files\J
    ava\jdk1.6.0_03\bin;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Dush\Workspac
    es\JavaWorkspace\OpenGTS\bin;
    Put the java home bin folder on the path before %SYSTEMROOT%\system32
    set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_03
    set PATH=%JAVA_HOME%\bin;%PATH%

  • Passing importing parameter in  ABAP to  java script variable

    Hi Experts,
    I am calling a ABAP function module in javascript.Now how can I take importing parameter in  ABAP to java script variable. Because I need to give alert using the improting Paramter value...
    Thanks in advance..
    RR

    to pass ABAP variable value to javascript variable the syntax is
    var myjsvariable = "<%=abapvariable%>";
    Edited by: Durairaj Athavan Raja on Jul 29, 2009 2:39 PM

  • Smart Forms : problem with Float Variable

    Hi  Expert,
       I have a probelm with Float variable. In my smart forms one fields of table QAMV contain value like 8.0000000000000000E+01.
    I have to remove the exponent and display value like 80.
    I have tried wa_qamv-sollwert(E10.4).. but it not working.
    Can U suggest me some solution.
    Regards
    Swati.
    Edited by: Swati Namdev on Apr 27, 2010 12:19 PM

    Hi,
    Please follow the steps.
    1. In your smart form create a variable w_val ,, type,, associate type -->  CHAR10
    2. Now write a code, jsut above your node where u wud like to get this value... For this Create -> Flow Logic -> Program Lines.. Now you have editor..
    3. In input parameters , put i_tab-QAMV.. and in output parameter put w_val... and Code like below
    CALL FUNCTION 'MD_CONV_QUANTITY_FLOAT_TO_PACK'
      EXPORTING
        iv_menge       = i_tab-qamv
      IMPORTING
        EV_MENGE       = w_val.
    4. Now grag and drop this w_val from global data field list...your this w_val will have the rounded value.. Pls try this and check..   It will work.
    Regards,
    Lokesh.
    Edited by: Lokesh Tarey on Apr 27, 2010 9:28 AM

  • ChiliBean Control error '800a03e8'  java.lang.NoClassDefFoundError

    hi all,
    http://forum.java.sun.com/thread.jspa?forumID=759&threadID=5019680
    in reference to the post above, i have a Win2003 server running Sun ONE with Chilisoft ASP plug-in. i did the suggestions there but am still getting the same error, to be exact:
    ChiliBean Control error '800a03e8'
    java.lang.NoClassDefFoundError: MyHelloWorld
    /myhelloworld.asp, line 3
    <html>
        <% Dim mhw, mhwString, mwh1
    REM    Set mhw = NewJavaObject("MyHelloWorld")
           Set mhw = Server.CreateObject("Chili.Beans")
           Set mhw1 = mhw.Construct("MyHelloWorld")
           Set mhwString = mhw1.myString
           Response.Write(mhwString)
        %>
    </html>
    public class MyHelloWorld {
         public String myString = "Hello World!";
    }hope you could help me out.
    thanks in advance :D

    hi all,
    http://forum.java.sun.com/thread.jspa?forumID=759&threadID=5019680
    in reference to the post above, i have a Win2003 server running Sun ONE with Chilisoft ASP plug-in. i did the suggestions there but am still getting the same error, to be exact:
    ChiliBean Control error '800a03e8'
    java.lang.NoClassDefFoundError: MyHelloWorld
    /myhelloworld.asp, line 3
    <html>
        <% Dim mhw, mhwString, mwh1
    REM    Set mhw = NewJavaObject("MyHelloWorld")
           Set mhw = Server.CreateObject("Chili.Beans")
           Set mhw1 = mhw.Construct("MyHelloWorld")
           Set mhwString = mhw1.myString
           Response.Write(mhwString)
        %>
    </html>
    public class MyHelloWorld {
         public String myString = "Hello World!";
    }hope you could help me out.
    thanks in advance :D

  • Getting precision length  for float data type

    Hi All,
    We trying to find the precision length of a float (126) data type. Are there any built in functions in oracle for finding the same? E.g: the function should return 5 for the value 1.23456 .
    Also, are there any table level operation to reduce the decimal precision of a float 126 field?
    Regards,
    Raj.

    Not sure if this is what you're looking for:
    select data_precision from all_tab_columns
    where owner = '<your schema name>'
    and table_name = '<your table name>'
    and column_name = '<name of column in table>';
    or are you looking for a particular find_precision_in_number() function...
    i can't think of one off the top of my head, but you can always do:
    length(substr(<colname>, instr(<colname>, '.')+1))

  • 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 can I get the float variable in 2 decimal point?

    after running the following simply manipulation program,
    14.85 should be returned, but in stead, 14.849999.
    how can i change the precision? i want the returned number in 2 decimal point.
    public class TestC {
    public static void main(String[] args) throws Exception {
                   float var = 0.00f;
                   var = (float)4.95 * 3;
                   System.out.println(var);

    There is absolutely no way that you can change the precision. It is fixed.
    If you want to display two decimals places (which is not the same as changing the precision) then you can use the following.
      float var = 0.00f;
      var = (float)4.95 * 3;
      java.text.DecimalFormat f = new java.text.DecimalFormat("0.00");
      System.out.println("var=" + f.format(var));

  • How do you set the decimal precision of a float?

    Hello,
    I am new to Java and have run into a problem. I am trying to truncate an array of floats and save them to a file. I want to save two decimal places with 0's ie: 0 as 0.00
    For example:
    I have a number 0.9772128 and I want to convert it and store it as 0.98, the following code seems to truncate it but there is a problem.
    float d = output_Weights;
    d = Math.round(d * 100) / 100.0;
    some of my numbers come out as .1 instead of .10 and I can't seem to figure out how to add the 0's on the end of them.
    Does anyone have any suggestions or a better way of getting the two decimal precision?
    Thanks in advance for any help you might be able to offer.

    And if you want to set the decimal precision when you convert the floating point number to text (which is what you are talking about when you say it ends with .1 instead of .10), you use a DecimalFormat object to do that.
    (By the way, isn't there supposed to be an apostrophe in Arc'teryx?)

  • Trouble with float variables

    Hello!
    I'm taking my first java class, and having some trouble. Our assignment is to "change your program so the variables you used to represent the x & y coordinates are declared as the float data type and change their default values to floats. Use the cast operator in your program so your program compiles and the washing maching displays on the screen."
    But, when I changed the variables from "int xval=150" to floats, it won't compile. Anybody have any thoughts? Here's my code:
    public class applet1 extends Applet {
    public void paint ( Graphics g ) {
    float xval=150.0f;
    float yval=150.0f;
    g.drawString ("Pete", 10, 10 );
    g.drawString ("Assignment3 September 18, 2002", 10, 25 );
    g.setColor(Color.white);
    g.fillRect(xval - 150,yval - 150,350,350);
    g.setColor(Color.black);
    g.drawRect(xval-150,yval - 150,350,350);
    g.drawLine(xval - 150,yval - 120,xval + 200,yval - 120);
    g.fillOval(xval,yval - 145,20,20);
    g.fillOval(xval - 105,yval - 145,20,20);
    g.fillOval(xval + 100,yval - 145,20,20);
    g.setColor(Color.lightGray);
    g.fillOval(xval - 100,yval - 100,250,250);
    g.setColor(Color.black);
    g.drawOval(xval - 100,yval - 100,250,250);
    g.setColor(Color.white);
    g.fillOval(xval - 90,yval - 90,230,230);
    g.setColor(Color.black);
    g.drawOval(xval - 90,yval - 90,230,230);
    g.setColor(Color.gray);
    g.fillRect(xval + 170,yval - 110,15,25);
    g.setColor(Color.black);
    g.drawRect(xval + 170,yval - 110,15,25);
    g.drawString ("Whirlpool", xval - 10, yval - 105 );

    You can't use floats when the method requires ints; cast is needed.

Maybe you are looking for