Jump to content

Understanding basics


zero_ZX

Recommended Posts

Hi,

I have never tried to work with math in PHP before, but what I'm going to do is very simple.. It will just be additions..

 

Let's say a company invoice a client for the amount of $13.5

Would it be that simple that i can have the 13.5 stored in a database, and then plus it with another invoice of $5.5 in the database, and then it will give 19?

 

I'm from Denmark, and it would be very likely that people write $13,5 (comma) instead of . (period/dot), is there any way i can convert commas into dots?

 

Thanks in advance :D

Link to comment
https://forums.phpfreaks.com/topic/210765-understanding-basics/
Share on other sites

Yes, you can just add them together. Beware of the limited precision in floating point arithmetic though. Like you can't express 1/3=0.333... precisely in decimal form, there are certain numbers that cannot be expressed finitely in binary. To illustrate this problem, try having a look at this topic.

 

To overcome these shortcomings, you can store the values as integers, so you have 1350¢ + 550¢ = 1900¢ = $19.

 

You can use BC Math or GMP to perform arbitrary precision mathematics.

 

As for the comma/period thing, you can do something like str_replace(',', '.', $value); on user input. number_format can handle the other way around.

Thanks a lot for your help Daniel!

 

I'm trying to query all my invoices' price, by doing this:

 

$result3 = mysql_query("SELECT total FROM fakture WHERE kunde=('$id')");
$inforow3 = mysql_fetch_array($result3);	

 

But i realized, how will i be calculating this? I have may array, but would can i calculate the numbers in it? I don't know what would be the smartest :P Any help is much appriciated.

Like this: 

SELECT SUM(total) FROM fakture WHERE kunde = '$id'

 

Have a look at some of the other aggregate functions as well.

 

Also, I would recommend you to keep all identifiers in your code in English regardless of the user interface language. This serves two purposes: 1) English only uses a-z, so you won't run into restrictions there, 2) Anyone will be able to read the code and understand what is going on.

 

By the way, it's spelled faktura (with an 'a') ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.