Jump to content

Handling Money with PHP


rcouser

Recommended Posts

Hello there,

I'm having a problem displaying money correctly with php. I have a field called "balance" with the type float(10,2), in this field I have a number store as "34.55" which I can go in and look at within phpmyadmin but when I echo this value on the front-end of the site it is displayed as "34.549999237061"

Can someone please help?

Regards.

Link to comment
https://forums.phpfreaks.com/topic/226442-handling-money-with-php/
Share on other sites

You should be using a DECIMAL data type to store your values AND you are probably doing something in your php code that treats the number as a floating point value which results in a floating point conversion error. What's your actual code that is retrieving and displaying the value?

Thanks for the reply, I am working with a prebuilt system that uses float for currency. To retrieve the value is use the follow:

<code>

$conn = dbConnect('query');

$sql = 'SELECT id, balance

        FROM user

        WHERE id = ?';

$stmt = $conn->stmt_init();

if ($stmt->prepare($sql)) {

    $stmt->bind_param('i', $_GET['id']);

    $stmt->bind_result($id, $balance);

    $OK = $stmt->execute();

    $stmt->fetch();

    $stmt->free_result();

}

 

echo $balance;

</code>

Thanks james but that's not what i'm looking for. I added it to the database as 34.55 and I need it to come out as 34.55 so that if u update the value but adding 0.30, it can go back into the database as 34.85. The problem with using money_format() or number_format() is that it adds punctuation such as commas and full stops.

Where does the 34.549999237061 come from?

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.