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
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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.