This question already has answers here:
Best data type to store money values in MySQL
(13 answers)
Closed 8 years ago.
Is this the right way to show the float number within the MySQL Database?
if ($a == "balance") {
$querys= "SELECT balance FROM users WHERE Username='$user'";
$results= mysql_query($querys);
$rows = mysql_fetch_row($results);
$bfloat = (float)$rows['balance'];
echo $bfloat;
}
in the MySQL database the column name balanace is float and as example at the current user the value is 1.73. So its answer would be 1.73
Bad news. The answer should be approximately 1.73. For that in typed languages separate decimal types are used (java BigDecimal). See Decimal type in php for the same in PHP.
Floating point is approximates our decimal representation, it would need infinite number of digits in the binary system (base 2). Just as 2/7 in base 10, whereas it is 0.2 in base 7.
Also it is better tp accustom oneself to use prepared statements http://xkcd.com/327/
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
I'm currently working on retrieving data from coinmarketcap API and I would like to limit the strings to 5. I retrieve the data and then multiply the value *1.07 and the result to MXN, but the string is long, like 25.65675734343, I want to limit that string, this is my echo:
echo $xrpprice*$rate*$fxrates['rates']['MXN'];
Use round:
$result = $xrpprice*$rate*$fxrates['rates']['MXN'];
echo round($result,2);
In PHP you can use number_format to control how many decimal places. Assuming you don't want to limit the whole number to just five characters.
echo number_format($numberToFormat, $numberOfDecimalPlaces);
Please better explain your question. Also, what have you tried?
This question already has answers here:
How to use MySQL DECIMAL?
(4 answers)
Closed 6 years ago.
so in my database i store my users earnings but they will not go any higher than 0.99999 i try and add 0.000001 to it but will not go over to 1.00000 for some reason the column is decimal(5,5)
here is my update
$fdfsdfdsfsdf = mysql_query("UPDATE users SET available_earning=available_earning+0.00012 WHERE id = '".$owner2."'")
or die(mysql_error());
what am i doing wrong? Sorry about my variable names and yes i will be updating it all to pdo soon
You've defined the column as DECIMAL (5, 5). That means 5 significant digits, with all five to the right of the decimal point. The term "significant digits" means all digits on both sides of the decimal point.
Short version: you need to redefine the column as DECIMAL (x, 5) where x > 5 if you want to store values >= 1.
This question already has answers here:
Best data type to store money values in MySQL
(13 answers)
Closed 6 years ago.
I'm working with PHP in Laravel 4 and MySQL and I'm not sure how to handle monetary values.
I want to display them, using this format :
120,50€
1.000,50€
The amount field is of type DECIMAL(12,2).
Should I save the amount like 12050 or like 120,50?
What I would recommend, is to use :
decimal Euro values (1000.50) in your database
cents (100050) in calculations
a formatted string (1.000,50€) in your display to the user
More details
For storing a field in a MySQL database, something like DECIMAL(12,2) makes perfect sense. This is a numeric value that has the correct percision (two decimal digits) and allows you to store any monetary value up to 9999999999.99.
Cents are great for calculations, because it allows you to treat your monetary values as integers in calculations, which is more practical to work with (especially for divisions and multiplications), and which is typically the level you want your monetary values to be rounded up or down to.
No matter how you store your data in the database or how you do your calculations, you want your users to see the amounts in a format they're familiar with. For example, US dollars would usually formatted as $1,000.50 and Euros as 1.000,50€.
This question already has answers here:
How should I do floating point comparison?
(12 answers)
Closed 7 years ago.
Rather than using an epsilon for float comparison, can you reliably compare two floats for equivalency by rounding them to the desired precision?
For example:
round($float, 3) === round($otherFloat, 3)
No. If your numbers are just barely on opposite sides of the value where the function will round up instead of down (a half-integer if you're round to the nearest integer), then they will round to different numbers no matter how close together they are.
This question already has answers here:
PHP float/double stored as MySQL DECIMAL
(2 answers)
Closed 8 years ago.
I have a table with a DECIMAL(10,6) column. I have a string representation of a float (e.g. 35.3123122). I am using php to insert the value. I used floatval($string) to insert and it did not work. I also tried to insert the string itself with no success. Do I need to format the float value exactly to match the 10,6 criteria. If so, How can I do this?
Thanks
Not sure if this is your problem, but the value of "35.3123122" has 7 digits after the decimal point. DECIMAL(10,6) is 10 digits in total with a maximum of 6 digits past the decimal point.