Problems with float

hi,
Im using float values to represent the price of products in a program. When a user selects a number of products to buy the total value of all the goods is shown, by adding all the float values together. the problem is that when i add all the floats i sometimes get a value greater than 2 signficant places:
e.g.
total = 23.34234
when i really only want
total = 23.34
Is there any way of restricting the units shown after the decimal place or is there a better way to represent the price of the produts?

is there a better way to represent the price of the produts?Yes, use an integral type (e.g. int) to represent the price in pence/cents/gopiks/whatever-is-your-smallest-unit. Binary floating point numbers only approximate most decimal numbers, so they will give you much grief. As a heavyweight alternative you might consider BigDecimal.
Edna's suggestion is the correct way to format floating-point numbers for display.

Similar Messages

  • Problem with Float.toString

    Hi,
    I have a small problem with wrapper class Float.
    For example
    float f = 99999999999999999f;
    f.toString(); returns a mentioned in the api doc: 9.9999999999999999E16
    while I wanted the original 17 times a 9,
    how can I achieve this??
    THX

    I tried to use the DecimalFormat class with the patter 17 * #, but the string it prints is no longer the same as in the original Float (17 * 9), now I get 17 chars but they are no all 9

  • Problem with floated elements in IE5.5

    I've apparently got a problem with a right sidebar float
    being dropped in a primitive version of IE and I can't figure out a
    remedy, as none are suggested in dwcs3's browser compatibility
    function. The site is lindafarronknapp.com... if anyone has any
    suggestions I would more than appreciate it...

    Thank you for the replies. What was happening is that the other audio files were being onverted by PPPro. This one was not. I moved the file to the desktop and once again placed it into the project. This time the yellow conversion line on the bottom right corner went to work and it works fine. It was some type of glitch with that one file. Thanks again. Roman

  • 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

  • Important Problem with floating toolbar !

    Hi all!
    I developed a plug-in using Acrobat 8 SDK. When I start acrobat 9.0,
    click any floating toolbar on which all tool buttons have been expanded,acrobat 9 will exist,but acrobat 8 is OK. This situation only happens when floating toolbar has some expandable tool buttons and all buttons have been expanded, and acrobat 9.0 starts without openning a PDF.
    I debuged my plug-in, stack overflow happened in acrobat.exe!
    How to solve this problem? Thanks all!

    I would start by using the debugger to try to find where the crash is taking place. I wold ensure that all of my code is properly wrapped with DURING/HANDLER blocks.
    If you are still having problems, I'll submit a formal support request to Adobe Developer Support.

  • Precision problem with float

    Hi,
    I have class with an amount variable whose type is float. If a value like 999999.99 is set to this variable, when I try to display this in JSP, the precision is lost. It displays 1000000. But my requirement is to print 999999.99. Is there any to solve this problem? The constraint to this prioblem is I can't change the data type of this variable. (Actually this class is generated through Castor. The xsd type is float. I am not supposed to change the XSD).
    Please advise.

    There's nothing to be done about it if you can't change the data type. See for yourself:    System.out.println(999999.99F == 1000000F);  // yep, they are equalBlame the person who decided that you should use float.

  • Access db: problem with float numbers if access isn't installed

    hi all,
    i wrote a simple application to store/retrieve float values into/from an access db.
    i tried it on my computer (with ms access installed) and i could store float values (ie 136.5), but when i tried my application (copying all classes and the db) on another machine without ms-access, i couldn't store float values, only int (ie 1365.0).
    the column "price" in the db is set as float with 1 decimal number, and there are other columns set as string, with no problem.
    anyone could help me?
    should i install access into the second machine too?
    thanks
    sandro

    Could it have something to do that on one machine '.' is interpreted as a decimal point, but on the other machine it is interpreted as the , you use in e.g. 1,000 (=1000).
    I would check that both macines use the same Regional Settings (in the control panel).
    Do you use any of the java.text.xxxFormat classes?

  • Problem with Float - round down error

    I have to save in database (MySql) amount which is passed by user. For instance user writes 99999999999. But Float.parseFloat(request.getParameter("amoUnit")) returns value 9.9999998E10 . When I save this value in database I receive 99999998000.00. I want to have in database exactly what the user has written. Should I use differen data type? Thanks in advance.

    leonkaP wrote:
    ... I want to have in database exactly what the user has written. Should I use differen data type? Yes.
    If you want more precision than float, use double.
    If you want exact precision, use BigDecimal.

  • Problem with floating point numbers

    Hi Experts,
    I am accepting two float values thru a method :- amount and tenure.
    Now wat i want to do is
    target = amount / ( tenure * 12 );
    but result target is not what it supposed to be. For example:-
    if amount = Rs. 15 00 000.0 and tenure = 3 yrs, target amount should be 5 00 000. but it is coming 41666 sumthing.
    How should I code this?
    Pls help,
    regards,
    Amey

    Hi,
    I am accepting two float values thru a method :- amount and tenure.
    Now wat i want to do is
    target = amount / ( tenure * 12 );
    but result target is not what it supposed to be. For example:-
    if amount = Rs. 15 00 000.0 and tenure = 3 yrs, target amount should be 5 00 000. but it is coming 41666 sumthing.
    How should I code this?
    I dont see any error in calculation,
    15 00 000.0 / 36 is perfecctly 41666 sumthing
    how can you expect this to be 5 00 000?
    If you want that change your calculation from
    target = amount / tenure ;
    Regards
    Ayyapparaj

  • Problem with float type display

    I have a float number, after bigger than 1E8,
    it will display as 1E8.
    How can I make it to display at 10000000?
    Thanks

    Hi!
    You may use something like:
    import java.text.*;
    double f = 123456789.9876;
    NumberFormat nf = new DecimalFormat("####.####");
    System.out.println(nf.format(f));
    I believe it is a good idea to use double for such numbers, not float.

  • Problem with floating windows CS4

    Since CS4 I don't see a possibility to open all images separatly in floting windows. When I change it at the pull down menu it just does it with the recent pictures as soon as I open a new one (from the desktop) they are "stacked" again.
    There must be a possibility to change that in general.I do work with PS since 8, this seams to be new. Ay ideas ???
    THX

    UNcheck the "Open Documents as Tabs" setting Tab in Photoshop > Preferences > Interface > Panels and Documents >. I've never seen tabbed windows on my machine thanks to that. ;)

  • Stumped on basic problem with floating point math

    I can't figure this out!  It should be sooooo simple. 
    Here is the challenge: 
    I have an incoming time array.  For example:     0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    I want to scale this array by a constant (e.g. multiply by 0.1).  So the resulting array should be:     0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9
    Then I want to calculate the difference between each and every subsequent element in the array.  In this example, the difference should 0.1 between every element.  But my comparison fails. 
    See the example below.  As far as I can see, the resulting boolean should always be TRUE.  But its not.
    But if I remove the scaling operation, then it works ok!
    Please help!
    Solved!
    Go to Solution.

    It has been awhile since smercurio has had a contribution to his retirement fund, but once again you have "discovered" that there is no exact binary representation for 0.1.
    I'd use one of the current "almost equals" comparisons described here:
    http://forums.ni.com/t5/LabVIEW/Darin-s-Weakly-Nugget-2-8-11/m-p/1444262
    And vote for this if you haven't already:
    http://forums.ni.com/t5/LabVIEW-Idea-Exchange/quot-Almost-Equal-quot-functions-for-Float-comparisons...

  • Problem with floating point precision

    Hello!
    I need to store in a variable a number like this 1234512345.123412345.
    But when I store it on a double or long double... it gets truncated to 1234512345.123412... only six decimals?
    What can I do??
    Thanks!

    If you are going to use the BigDecimal class, it is worth bearing in mind a tip that was passed on to me from georgemc. If you create the BigDecimal class instances using the constructor that accepts a String as input, then you can easily specify how many digits should follow the decimal point.

  • Problem with TLF - floating an inline image

    I am running the latest version of Flash Builder 4 (4.0.1).  The problem that I having:
    When I use an InlineGraphicsElement if aTextFlow (in a RichEditableText component), if I set a value for the "float" attribute of an <img> element; the image is not visible.  Without the float attribute, I see the image.
    Is this a version problem with TLF?  How do I know which version of TLF that I'm using?  How do I install the correct version of TLF?
    Thanks for the help.
         Oz
    Some code fragments:
    The TextFlow string with "float":
    <div color="#444444" fontFamily="Times New Roman" fontSize="20" paragraphSpaceAfter="15" textIndent="10">
    <p><span color="0xff0000" >Alice, </span><span>really, was </span><span color="0xff0000" >beginning </span><span>to </span><span color="0xff00" >get very </span><span>tired of </span><span color="0xff00" >sitting </span><span>by her </span><span color="0xff00" >sister </span><span>on the </span><span color="0xff00" >bank, </span><span>and of </span><span color="0xff00" >having nothing </span><span>to do:  once or twice she had peeped into the book her </span><span color="0xff00" >sister </span><span>was reading, but it had no </span><span color="0x888800" >pictures </span><span>or </span><span color="0xffff" >conversations </span><span>in it, `and what is the use of a book,' thought </span><span color="0xff0000" >Alice `</span><span>without </span><span color="0x888800" >pictures </span><span>or </span><span color="0xff00" >conversation?'</span></p>
    <p>
    <img float="left" width="100" source="assets/library/alice/images/White Rabbit.png" height="100"/>
    <span>So she was </span><span color="0xffff" >considering </span><span>in her own mind</span><span>(</span><span>as well as she could, for the hot day made her feel </span><span color="0xff00" >very </span><span>sleepy and stupid), whether the pleasure of making a daisy-chain would be </span><span color="0x880000" >worth </span><span>the trouble of getting up and picking the daisies, when suddenly a White </span><span color="0xff00" >Rabbit </span><span>with pink eyes ran close by her.</span></p>
    </div>
    Without "float":
    <div color="#444444" fontFamily="Times New Roman" fontSize="20" paragraphSpaceAfter="15" textIndent="10">
    <p><span color="0xff0000" >Alice, </span><span>really, was </span><span color="0xff0000" >beginning </span><span>to </span><span color="0xff00" >get very </span><span>tired of </span><span color="0xff00" >sitting </span><span>by her </span><span color="0xff00" >sister </span><span>on the </span><span color="0xff00" >bank, </span><span>and of </span><span color="0xff00" >having nothing </span><span>to do:  once or twice she had peeped into the book her </span><span color="0xff00" >sister </span><span>was reading, but it had no </span><span color="0x888800" >pictures </span><span>or </span><span color="0xffff" >conversations </span><span>in it, `and what is the use of a book,' thought </span><span color="0xff0000" >Alice `</span><span>without </span><span color="0x888800" >pictures </span><span>or </span><span color="0xff00" >conversation?'</span></p>
    <p>
    <img width="100" source="assets/library/alice/images/White Rabbit.png" height="100"/>
    <span>So she was </span><span color="0xffff" >considering </span><span>in her own mind</span><span>(</span><span>as well as she could, for the hot day made her feel </span><span color="0xff00" >very </span><span>sleepy and stupid), whether the pleasure of making a daisy-chain would be </span><span color="0x880000" >worth </span><span>the trouble of getting up and picking the daisies, when suddenly a White </span><span color="0xff00" >Rabbit </span><span>with pink eyes ran close by her.</span></p>
    </div>
    A bit of MXML code:
        <s:Group id="myGroup" width="100%" height="100%">
            <s:RichEditableText id="myRichText" lineBreak="toFit">
            </s:RichEditableText>
            <!--- Do not set the height of the RichEditableText - since it seems to prevent the appearance of the vertical scroll bars -->
        </s:Group>
    And some Actionscript that adds components:
                scroller = new SxScroller();
                scroller.dx = dxScroller;
                scroller.dy = dyScroller;
                scroller.addEventListener(FlexEvent.CREATION_COMPLETE,scrollerEvent);
                scroller.addEventListener(FlexEvent.UPDATE_COMPLETE,scrollerEvent);
                richText = scroller.richText;
                richText.editable = false;  // textCharacter.enableEdit; // probably false.  TODO:  support true???
                richText.selectable = true; // required
                richText.width = dxRichText;
    And finally the import of a TextFlow string:
    richText.textFlow = TextFlowUtil.importFromString(textFlowString, WhiteSpaceCollapse.PRESERVE);

    On the TextFlow forum I found the answer:
    Use Flash Builder Hero, since it support TLF 2.0 out-of-the-box.  The InlineGraphics support several valuable attributes, including float="left" etc.
    Beautiful.

  • Problems with the floating lens.

    Hello Nokia,
    My Lumia 920 has a very wierd problem with the floating lens. When I tilt the device up it blurrs the image for a sec or two and it makes a squirk sound which also records on the footage.
    Here is a video made with my Lumia 920.
    http://youtu.be/jlzzOBlAT6Q
    I sent my phone for a replacement, but I just wanted to let you know that this could happen and maybe stimulate those little chinese people in your factories to do a better job.

    Hello Nokia,
    My Lumia 920 has a very wierd problem with the floating lens. When I tilt the device up it blurrs the image for a sec or two and it makes a squirk sound which also records on the footage.
    Here is a video made with my Lumia 920.
    http://youtu.be/jlzzOBlAT6Q
    I sent my phone for a replacement, but I just wanted to let you know that this could happen and maybe stimulate those little chinese people in your factories to do a better job.

Maybe you are looking for