Float & double variables

I need to calculate money values, and was wondering how you get the float or double value to return only two characters after the decimal point?!

Using float or double is not the best way for
handling money. You may loose some....
What about using BigDecimal?
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-
cents.htmlActually, he's probably converting money types (like dollars to pounds or whatever), and the teacher probably doesn't care about that minute error in calculation.
But yeah, it's like my comp-arch teacher said. "If I have ten million dollars in the bank, and every second I add in my interest for that second, how much money do I have a year later?"

Similar Messages

  • How to store  double  variable in hash map

    i need to store double variable using hash map, but i cant able to store it
    my jsp coding contains
    double et=24,j=5;
    hm.put("stm",st);
    hm.put("etm",et);
    Generated servlet error
    C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\exam\org\apache\jsp\availability_jsp.java:752:
    put(java.lang.Object,java.lang.Object) in java.util.Map cannot be applied to (java.lang.String,double)
    hm.put("stm",st);
    ^
    how to overcome this problem
    thank u in advance

    double etme;
    etme = hm.getDouble("etm");
    i'm getting this error
    C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\exam\org\apache\jsp\editdel_jsp.java:85: cannot resolve symbol
    symbol : method getDouble (java.lang.String)
    location: interface java.util.Map
    etme = hm.getDouble("etm");
    ^
    how to solve it
    plz help me

  • Float/double implementation

    I am writing an application in J2ME and badly need a float/double data type, which J2ME does not have. I was wondering if anybody had some good ideas on how to implement a float/double data type using only ints.
    Thanks.
    AnjuM

    Use 2 longs, one for the 1's and one for the deciamal part. if the decimal part is over the max number you want to represent in decimal then subtract max from it and add 1 to the 1's side.
    If I wanted to show 9 decimal places, then anything past 999,999,999 should have 1,000,000,000 subtracted from it and 1 added to the left side.

  • Retrieving parameter and assign it to double variable

    I have quick question.
    Can I retrieve parameter value and assign it to a previously
    defined double variable?
    For example, if there is a previously defined
    double variable named cost and a following tag
    <PARAM NAME=price VALUE=10.00>
    can I just do
    cost = getParameter("price");
    to retrive the value?
    Thanks

    Hi Kei,
    No you can't do
    cost = getParameter("price");because method "getParameter()" returns a "String" (not a 'double').
    But you can do something like this:
    String price = getParameter("price");
    double cost = Double.parseDouble(price);Note that the above is incomplete. The "parseDouble()" method may throw an exception. PLease check the relevant javadoc for more details.
    Hope this helps.
    Good Luck,
    Avi.

  • Setting precision value of float/double

    Hi,
    I am retrieving data from database in to a float/double datatye. when i display the value it is showing the decimal of 4 to 5.(ex:4325.65785) but i want to display it as (ex:4235.65).
    Thanks
    Gopi

    import java.text.DecimalFormat;
    public class FormatTest
         public static void main(String[] args)
              DecimalFormat df = new DecimalFormat("##0.00");
              double d = 123456.2345678;
              System.out.println("Formatted: " + df.format(d));

  • JDev 9.0.3.3 - float/double in the debugger

    Hello!
    I use JDeveloper ver. 9.0.3.3 and during last debug session I noticed one strange behaviour in the JDev debugger.
    There is a variable of type float with value of 10964.17. I see the value when I do System.out.println() of the variable. But the debugger show me value of 10964.2 !!!
    It seems like the debugger rounds the value. Also, I noticed the same behaviour with doubles too.
    What you think about it?
    I'll try to prepare a short test for the issue - when I'll have time for it.

    No need to prepare the test. I can reproduce it easily from your description.
    It only happens when debugging with OJVM. I've entered bug #3455484.
    Sorry for the bug.
    Thanks for the report.
    -Liz Looney

  • Float double big problem

    New to java.
    Heres a perplexing issue:
         private Float lengthInFeet;
         private Integer lengthInInches;
         public void calcLengthInFeet()     
              lengthInFeet = lengthInInches * 1.0 / 12.0;
    compile error:
    incompatible types
    found : double
    required: java.lang.Float
    lengthInFeet = lengthInInches * 1.0 / 12.0;
    What am I doing wrong?
    Thanks,
    sb
    ^

    You are mixing primitive types and class types.
    You need to know the difference.
    To create a new Float object, you have to use the new operator.
    Try reading the section on variable types in the online tutorial:
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html
    You can also fix this single problem by changing the class types to primitive types, like this:
    private double lengthInFeet;
    private int lengthInInches;However, unless you learn/understand the distinction between the types, you won't get much farther. Read the tutorial.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Float / Double Decimal Display

    How do you get a float or double to display a certain amount of decimal places?
    For example, dealing with money, to have it be displayed as $1.30 instead of $1.3

    Use NumberFormat class!
    A simple example:
    NumberFormat currency = NumberFormat.getNumberInstance();
    currency.setMinimumFractionDigits(2);
    currency.setMaximumFractionDigits(2);
    System.out.println(currency.format(AnyValue));

  • Float/double data types

    I use
    public void setId(float id) {this.id = id;}
    and
    setId(new Float(nodeNextTop.getChildNodes().item(0).getNodeValue()).floatValue());
    in my java files to get the value of a float which I want to display using
    <display:column align="center" sortable="true" nowrap="true" property="id" title="<%=id_%>" class="webText" />
    in my jsp file
    but it is displayed in true E format, ie, 1.123456789101112E15, I want to display the number as is, without the E, and it's beaten me, pls help.
    btw, double does the same

    top job, I'm learning something new each day, big thnx for the help

  • How to roundoff addition of two double variables

    class Example
         public static void main(String args[])
              double k=1.236+2.138;
              System.out.println(k);
    }output of program:
    3.3739999999999997
    so how will i get the exact value of the addition

    etgohomeok wrote:
    If you know it's going to be 3 decimal places every time, you could always do:
    double d1 = 1.236;
    double d2 = 2.138;
    int i1 = (int) d1 * 1000;
    int i2 = (int) d2 * 1000;
    int iResult = i1 + i2;
    double dResult = ((double) iResult) / 1000;You could collapse that into a couple of lines and eliminate alot of the variables, but that's the general point...Unfortunately, that gives the wrong answer. Try running it and see.

  • ProC cannot insert float/double C datatype

    Hallo,
    we compiled our C-Programms which we developed on SCO OpenServer
    with the ProC on Linux.
    Like on SCO we use the LANG enviroment variable de_DE for
    german national language support. If we try now this C-Code:
    setlocale(LC_ALL, "");
    double x = 2.34;
    EXEC SQL INSERT INTO anytable VALUES (:x);
    EXEC SQL COMMIT;
    (The table anytable has one column with number(10,4) )
    The result is that the value in table is only 2 instead of
    2,34 (german decimal separator is ,).
    If we try setlocale(LC_ALL, "C") the value in the table is
    correct 2,34.
    But we have to use the german national language
    support with setlocale(LC_ALL, "") for other purposes.
    So is it a bug or did we miss something.
    (On SCO it works with setlocale(LC_ALL, "");)
    Thanks !
    Reinhold Berger
    null

    i think you need to debug your code, lookalike the values you trying insert already exist in the database.
    these two IDs (6323df8a-5c57-4d3e-a477-09aa8b66100a, 7ae114df-9d52-4b08-affa-8c544cbc27b6).
    i would try to run the select command against the content db.
    SELECT TOP *  FROM [DB Name].[dbo].[NavNodes] where id = '6323df8a-5c57-4d3e-a477-09aa8b66100a'
    Please remember to mark your question as answered &Vote helpful,if this solves/helps your problem. ****************************************************************************************** Thanks -WS MCITP(SharePoint 2010, 2013) Blog: http://wscheema.com/blog

  • Float - double == loss of accuracy?  why?

    The following test fails:
    public void testFloat() {
            float number = 46702.45F;
            Float numberAsFloat = new Float(number);
            double numberAsDouble = numberAsFloat.doubleValue();
            System.out.println(numberAsDouble);
            assertTrue(46702.45 == numberAsDouble);
    }I probably just don't understand floating point data types or something, but I was very surprised that this test fails. If someone could enlighten me on WHY this fails, I would really appreciate it. For what it's worth, the value printed out for numberAsDouble was 46702.44921875 which I find entirely unexpected considering I am going from a lower precision data type (float) to a higher precision data type (double).

    Remember: the floating point representations (float and double) store binary values. Not decimal. The decimal values are parsed by the compiler and turned into floats and doubles in binary.
    The said binary values are approximations to the decimal values. Sometimes the binary and the decimal values coincide, but some times they don't.
    Floats have less precision than doubles.
    The binary string that represents that number, in a float, will be interpreted differently in a double, because the double has those additional bits of precision.
    Consider this code:
        double nd = 46702.45d;
        float nf = 46702.45F;
        long ndl = Double.doubleToLongBits(nd);
        long nfl = Float.floatToIntBits(nf);
        long nfl2 = Double.doubleToLongBits(nf);
        System.out.println(Long.toBinaryString(ndl));
        System.out.println(Long.toBinaryString(nfl));
        System.out.println(Long.toBinaryString(nfl2));It prints this:
    100000011100110110011011100111001100110011001100110011001100110
    1000111001101100110111001110011
    100000011100110110011011100111001100000000000000000000000000000
    // now I add spaces to line up the various segments
    // sign   exponent  mantissa
         1 00000011100  110110011011100111001100110011001100110011001100110
         1    00011100  1101100110111001110011
         1 00000011100  110110011011100111001100000000000000000000000000000Look at that string of "0011"'s on the version of the number created as a double.
    Compare it to the 0's in the version that was created as a float and used as a double.

  • MIDP - Float, Double - missing? Workaround?

    Hi
    i tried to use float or double in my first J2ME Project but it doesn't seem to work (although the compiler compiles the source throwing no exception, the application fails to run..).
    I had a look at the MIDP 1.0 specification and think i found that float and double are not supported. Is taht really true?
    If so, it seems a little bit strange to me, because these are basic primitiv data types that have a wide usage field.
    In fact, i need to deal with floating point values in my J2ME, so, if there is noting like float or double, what could i do? Is there any workaround (only one i see is to store the 'float' as an 'extended' integer?
    Thanks for your help,
    josh

    hi...
    J2ME does not support floatting point (means both float and double) yet however you can cheack this site for alternative solution:
    http://wireless.java.sun.com/midp/ttips/fixpoint/
    or you want to download a good math float emulator for it (for free) at:
    http://www.jscience.net/
    :-)

  • Float/double confusion

    Could someone explain the theory behind the following:
    The default floating point data type is a double, hence the following declaration will not compile:
    float f=1.3;
    However the following declaration will compile:
    float f=1/3
    ...without specifying that it should be a float. Why does this happen, and why is the resulting value in f 0.0? Surely it should be 0.3333 or similar?

    1/3 is the integer 1 divided with the integer 3, and since they both are integers it would be natural for the result to be integer too, and so the division performed is called integer division. And the integer 0 is the result because one third is doesn't fit any integer. The fractional part is clamped. This result is then implicitely casted to produce the float 0.0f.
    Why? Because that's the way it goes in C and C++.

  • Division in java double variable.

    Hi all
    I have a simple query .I am using the following code in my java mapping.
    Please follow the following java code
    double d1 = 22058310.53;
    System.out.println("Output Value = " + d1/1000);
    This gives me an output
    Output Value = 220583.10530000002
    whereas the correct o/p should be 220583.1053
    Can anyone pls provide a solution to get the correct result.
    regards
    NIlesh Taunk.

    Hi Nilesh,
    Use the following code to Divide by 1000 using BigDecimal class which is better than using decimal type.
      <b>BigDecimal num= new BigDecimal("22058310.53");
      BigDecimal result= num.movePointLeft(3);
      System.out.println("Output Value = " + result.toString());</b>
    OR
      <b>BigDecimal num= new BigDecimal("22058310.53");
      BigDecimal result= num.divide(num,BigDecimal.ROUND_HALF_UP);
      System.out.println("Output Value = " + result.toString()); </b>
    Hope this helps.
    Regards,
    Ananth

Maybe you are looking for