Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7770303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:22:38+00:00 2026-06-01T16:22:38+00:00

When summing a group of numbers sometimes I end up with some low decimals?

  • 0

When summing a group of numbers sometimes I end up with some low decimals? Why can it happen when the numbers are parsed as strings? I know there is some %”&! about floats

function parse(){
    foreach($_SESSION['import_csv_posts']['result']['csv'] as $key => $post){
        $amount = $this->parse_amount($post[$this->param['amount']]);
        if($this->param['vat_amount']){
            $amount += $this->parse_amount($post[$this->param['vat_amount']]);
        }

        $this->balance += $amount;
        echo "$amount\n";
    }

    echo "\nbalance = ".$this->balance;
}

function parse_amount($amount){
    $amount = strval($amount);
    if(strstr($amount, '.') && strstr($amount, ',')){
        preg_match('/^\-?\d+([\.,]{1})/', $amount, $match);
        $amount = str_replace($match[1], '', $amount);
    }

    return str_replace(',', '.', $amount);
}

result

-87329.00
-257700.00
-11400.00
-9120.00
-47485.00
-15504.00
122800.00
1836.00
1254.00
200.00
360.00
31680.00
361.60
1979.20
1144.00
7520.00
6249.49
balance = -399.00000000003
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:22:39+00:00Added an answer on June 1, 2026 at 4:22 pm

    The “%”&! about floats” is that floats are simply not precise. There’s a certain inaccuracy inherent in how infinite numbers are stored in finite space. Therefore, when doing math with floats, you won’t get 100% accurate results.

    Your choice is to either round, format numbers to two decimal places upon output, or use strings and the BC Math package, which is slower, but accurate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I count to 10 while summing the numbers in VBA?
Thanks to some help I received yesterday I've got some dynamic summing working on
On a PHP & CodeIgniter-based web site, users can earn reputation for various actions,
I have a project where i get some values over some entities and summing
I have a Java method in which I'm summing a set of numbers. However,
The short: A simpler way of summing up this question would be, can you
Summing up, my problem consists on compiling g95 objects inside a C++ application. Actually,
I want to do something similar to summing 1/x from 1 to 100 without
I'm working on UVa #112 Tree Summing . I have what I think should
I'm searching for an algorithm for Digit summing. Let me outline the basic principle:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.