Rounded Numbers Dont Add Up

I have a user who is complaining that the numbers on her report don't add up to the total that's printed at the bottom. I haven't seen a sample of this yet but I wanted to run this by you guys and see if this code makes sense.
  function() {
    NumberFormat nf = new DecimalFormat("#,##0.00");
    double total = 0D;
    for (...) {
      double d = Math.random();
      total += round(d);
      print(nf.format(d));
    print(nf.format(total));
  double round(double d) {
    return (double)(Math.round(d*100)/100D);
  }Is my round() sound?

I have a user who is complaining that the numbers on
her report don't add up to the total that's printed at
the bottom. This could be a problem with the user in that they don't understand numerics.
Or it could b a problem with you in that you do not understand the problem domain.
Floating point numbers are always inaccurate. It doesn't matter whether humans or computers do this. That is because floating point numbers (when used correctly) are an approximate measure of something which can not be measured exactly.
For example a pound of hamburger might be marked as weighing 1.01 pounds. The inaccuracy is in that it might actually weigh 1.01015678 pounds (but is unlikely to weigh even that.) There is no way to get the exact weight.
Now if that is your situation then it is problem with the user - the user doesn't understand numerics and the inherit inaccuracies.
And it would probably be a bad idea for you to "fix" it for one user, when other users are expecting correct approximations.
(One way to fix this is to 'fiddle' with the numbers that make up your sum. Adjust one so everything 'looks' right.)
On the other hand maybe it is your problem. For example money. Money is not a floating point number, because money is always exact. Even when caculating interest it is always exact. You will never end up with a partial penny in your savings account, no matter how the interest rate is computed.
And since money is accurate a floating point number is not necessarily the best way to represent it. Care must be used in doing so. As a previous poster suggested the BigDecimal class can be used.
Or if your numbers are small then simply convert the money to integers by multiplying by 100 and rounding and then compute everything using that. And simply reformat the integer values so they look like floating point numbers rather than integers when you print them out.

Similar Messages

  • How to round numbers

    assume variable a is 4.647 and i want to convert this to 4.5 . how can i round numbers like this. Is there is a predefined method in java to achive this.

    I don't think java.lang.Math.round() will do what the original post asked for, since it rounds to the nearest long number. So you still would have to write your own method to round 4.647 down to 4.5.

  • How to round numbers using javascript in Adobe Acrobat Pro?

    How to round numbers using java script in Adobe Acrobat Pro?
    For example:
    1.2 becomes 1.0
    1.7 becomes 2.0
    Thank you.

    Assuming you've already set the field to a Number format category and limited it to one digit to the right of the decimal, you can use the following custom Validate script:
    // Custom Validate script
    event.value = Math.round(event.value);
    More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/roun d

  • I have a new iPad with wifi. When I send messages I can see my sons and my husbands but not my phone number.  We all have an iPad and all use the same apple ID as iPad the bill.  How can I remove their numbers and add mine?

    I have a new iPad with wifi. When I send messages I can see my sons and my husbands but not my phone number.  We all have an iPad and all use the same apple ID as iPad the bill.  How can I remove their numbers and add mine?

    Add another e-mail account to your messages, so people could reach you on that e-mail from messages. As soon as you are online (if you have wifi only iPad) you are able to send and receive messages. i.e. your son can send you a messages from his iPhone addressing it to your (that additional) e-mail address.

  • I am using Numbers on my iPhone5 and cannot get the app to do a simple (SUM) calculation.  It shows the formula correctly in the cell, but all I get for my long list of numbers to add is 0.  How can I get this to work?

    I am using Numbers on my iPhone5 and cannot get the app to do a simple (SUM) calculation.  It shows the formula correctly in the cell, but all I get for my long list of numbers to add is 0.  How can I get this to work?

    Oaky, at least we got that far.  Next step is to determine if all those "numbers" are really numbers.  Changing the format to "number" doesn't necessarily change the string to a number. The string has to be in the form of a number.  Some may appear to you and me as numbers but will not turn into "numbers" when you change the formatting from Text to Number. Unless you've manually formatted the cells to be right justified, one way to tell if it is text or a number is the justification. Text will be justified to the left, numbers will be justified to the right.
    Here are some that will remain as strings:
    +123
    123 with a space after it.
    .123

  • Siri's voice just says everything that i touch. The voice says the numbers i touch but the numbers dont show up on the screen so i cant make phone calls! what is going on??

    Siri's voice just says everything that i touch. The voice says the numbers i touch but the numbers dont show up on the screen so i cant make phone calls! what is going on??

    It sounds as if you have Voiceover activated. You should be able to turn it off by triple tapping the Home button.

  • HT203128 iTunes 12.1.050 - Rollback: Updated from 12.01, now I cant't change track numbers and add additional data fields. How can I roll back to the previous version? @Apple, I want a $100 gift card for the inconvenience.

    iTunes 12.1.050 - Rollback: Updated from 12.01, now I cant't change track numbers and add additional data fields. How can I roll back to the previous version? @Apple, I want a $100 gift card for the inconvenience.
    - it's all said in the title. I can't understand why the developers turn things bad ... risking loosing Apples valuable customers.
    More examples needed? (check out the communities and the reviews)
    - OSX: Maverick to Yosemite = not recommended (check it out > http://roaringapps.com/apps)
    - iPad: older versions as iPad2 should not upgrade to iOS7/8 (it's not supporting its hardware accordingly, it's designed for the latest hardware)
    - iTunes (it's possible to rollback to 10.7 - 11.4, but you need to breech OSX system security framework)
    - Pages (was a real alternative for many of us, each update reduced features and created incompatibility to older work files)
    The list could easily extend to ...

    You can offer Apple feedback here: http://www.apple.com/feedback/
    as for "
      "@Apple, I want a $100 gift card for the inconvenience"
    dream on.

  • Calculations with Rounded Numbers

    I've created a form in Adobe X that has a few calculated fields.  For ease of conversation, we'll call them Field A, Field B and Field C (see below).  I need Fields A and B to be rounded to the nearest whole number so that Field C is also a whole number.  I've tried formatting Fields A and B to show no decimals but the calculaton is still being performed on the non-rounded numbers.  Can anyone help with this?  Thanks.
    Field A - Field B = Field C

    George I must be missing something.  I added the math.round functions as shown in your example but now it doesn't calculate at all.  Can you look at the coding shown below and let me know where I went wrong?  Thanks.
    Updated Coding:
    (function(){
    var totalMiles = math.round(+this.getField("Total Miles DrivenRow1").value);
    var homeMiles = math.round(+this.getField("Home Base MilesRow1").value);
    var diff = totalMiles - homeMiles;
    if (diff<0) diff = 0;
    event.value = diff;})();
    Original Coding:
    var totalMiles = +this.getField("Total Miles DrivenRow1").value;
    var homeMiles = +this.getField("Home Base MilesRow1").value;
    var diff = totalMiles - homeMiles;
    if (diff<0) diff = 0;
    event.value = diff;

  • How to round numbers in java?

    hey everyone. just wondering how i can round my final answer which is "repayment" located on the last line to the nearest whole value. thanks :) this is my code:
    //Conversion and Calculation
    principle = reader.readDouble("Enter Loan Amount: ");
    interestRate = reader.readDouble("Enter YEARLY interest rate: ");
    convertedRate = interestRate / 100;
    monthlyRate = convertedRate / 12;
    timePeriod = reader.readDouble("Enter the number of years for the repayment: ");
    convertedPeriod = timePeriod * 12;
    //Calculation Monthly Repayment Amount for a fixed number of years
    topHalf = Math.pow((1+monthlyRate),convertedPeriod);
    bottomHalf = (Math.pow((1+monthlyRate),convertedPeriod)-1);
    fraction = topHalf / bottomHalf;
    repayment = principle * monthlyRate * fraction;
    writer.println ("Fixed Monthly Repayment Amount = $" +repayment);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Good point: Klue2k, don't forget about the negative
    case.Yes, never forget about the negative case. Your
    solution doesn't work with negative numbers!
    And by the way, if you had a little bit more of the
    good old klue2k, you wouldn't be embarassing yourself
    in this way.I think you're confusing me with somebody who give a 5h!t.

  • Rounding numbers to 2 decimal places

    Hello programming community,
    I have the following code to round my answers to 2 decimal places:
    double roundedValue;
    DecimalFormat twoDForm = new DecimalFormat ("#.##");
    roundedValue = Double.valueOf(twoDForm.format(value));The code works great and what i expect it to do. When I pass in really big decimal numbers (2.55555668) it correctly rounds it (2.56) but when it rounds the number and the last decimal is a zero it does not display this. (if the number ends up rounding to 4.50 , i want it to display it this way but it ends up getting rid of the 0. )
    If anyone has any ideas for a way to fix this please let me know.
    Thanks.

    the-java-guy-needs-help wrote:
    i thought that my equation would be causing a problem and that i might need to do a cast -- I relized that this is not the problem
    for the type of string i am using the ("0.00")
    After trying various things the string does work if im just outputing it to the screen but i also use this rounding in a method that needs to return a double so i still need to convert it to a double.
    I have tried various ways of converting string to double (Double.parseDouble) and i tried one that involved a trim() but both methods drop the zero.You've got a basic misconception still going on here. If you convert it to a double, there is no such thing as a trailing zero -- it's meaningless since it only has meaning for a String representation of a double and not the intrinsic number itself.

  • When i add some songs they dont add and another thing

    when i add some songs the space amount will sometimes jump from eg. 12.45 to 12.51 and the file size is only 4mb. than after that it will jump back down to 12.46 and continue like nothing has happened. than i go to listen to some songs old/new on itunes or my ipod and on itunes it shows ! which i know means its gone but i dont know how and on the ipod it goes on it but skips. any help thanks

    If I understand you correctly, iTunes is not transferring songs to your iPod. Before you sync again, make sure that the selections you want transferred are selected (by clicking the box next to each selection from your library in iTunes). Also try this, when your ipod is connected, click it under devices and select 'Music'. Then make sure 'Sync Music' and 'All Songs and Playlists' are selected. Good luck
    Dell   Windows XP  

  • Creating x random numbers that add up to a certain number

    How would I go about creating 3 random numbers from x - y that add up to a certain number.
    Or how would i create a set of numbers that are random but not repeating.

    How would I go about creating 3 random numbers fromx
    - y that add up to a certain number.Create 2 random numbers. Number 3 = certain
    number - (random number 1 + random number 2)
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Rand
    om.html
    Or how would i create a set of numbers that are
    random but not repeating.Create a List and fill it with numbers 1 - n. Shuffle
    the list with the Collections.shuffle(...) method.
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Coll
    ections.htmlOr use a Set instead of a List, since Sets do not contain duplicates.
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html
    ? {?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How to round numbers in RTF templates?

    Hello!
    I'm using BI Publisher for Word (Oracle BI Publisher Builder for Word 10.1.3.4.1) to build a template to my BI Publisher report and I want to calculate this:
    avg(X)/(avg(Y)-avg(Z)/0.9)
    The result should be rounded to 3 places.
    I tried:
    <?(sum(X) div count(X)) div ((sum(Y) div count(Y)) - (sum(Z) div count(Z)) div 0.9)?>
    and I obtained the expected result.
    But, when I try to round in this way:
    <?xdofx:round((sum(X) div count(X)) div ((sum(Y) div count(Y))-(sum(Z) div count(Z)) div 0.9),3)?>
    I obtain 0 when is expected other value.
    Can someone help me?

    Hi ,
    You can select the number format in RTF template .Double click the Form Field select
    Type :Number
    Number Format :#,##0.000
    Thanks.

  • How to round numbers to the nearest .25

    I would like to enter a number in one field and have it rounded to the nearest .25 in another column - (whether that's rounding up or down)
    Example:
    Actual Size
    Adjusted Size
    7.21435
    7.25
    Any help would be appreciated.
    Thanks!

    Hi c,
    The ROUND function limits the precision choices to powers of 10, so we need to work within that limitation:
    Multiply by four, round to the nearer whole number, divide by four.
    Formula in column B: =ROUND(A*4,0)/4
    All cells in B are formatted as Number, with decimal places set to 2.
    Regards,
    Barry

  • HT1329 How do you deauthorize ipods that no longer work or have been stolen. What does it mean that you only have 5 devices that can download your itunes library. How do you deactivte old serial numbers and add new ones as of now we only have one ipod

    Over the years we have owned many i-pods. Most of them ran out of juice or were just outdated some even stolen. As of right now I have one i-pod that should be authorized to download music from my library. I plan to get a new one. My question is how do you deactivate all the old i-pods so that i-tunes doesnt think I have all these pods downloading from my personal lbrary of music. If I get a new one and plug it in it will say you are authorized 1 or 2  or whatever it is at this point to download music from this libtary. How to I get rid of all the old devices (serial numbers) that don't draw from my i-tunes library anymore?

    Ipods/iphones/ipads/apple tvs are not authorized at all.
    Authorization applies ONLY to computers.

Maybe you are looking for