zero_ZX Posted August 15, 2010 Share Posted August 15, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/210765-understanding-basics/ Share on other sites More sharing options...
Daniel0 Posted August 15, 2010 Share Posted August 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210765-understanding-basics/#findComment-1099474 Share on other sites More sharing options...
zero_ZX Posted August 15, 2010 Author Share Posted August 15, 2010 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 Any help is much appriciated. Quote Link to comment https://forums.phpfreaks.com/topic/210765-understanding-basics/#findComment-1099489 Share on other sites More sharing options...
Daniel0 Posted August 15, 2010 Share Posted August 15, 2010 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') Quote Link to comment https://forums.phpfreaks.com/topic/210765-understanding-basics/#findComment-1099492 Share on other sites More sharing options...
zero_ZX Posted August 15, 2010 Author Share Posted August 15, 2010 I see, thanks for the help, and thanks for the tip lol.. Client insisted that everything should be danish.. but even that i fail lol Quote Link to comment https://forums.phpfreaks.com/topic/210765-understanding-basics/#findComment-1099494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.