Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9309555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:53:43+00:00 2026-06-19T00:53:43+00:00

I have some prices and discount amounts that will be stored in config variables

  • 0

I have some prices and discount amounts that will be stored in config variables in PHP code. I’m wondering if I need to store them as strings.

For example, is it safe to do the following?

$price = 14.95;
$volume_discount = 0.3;

or should I store them as strings?

$price = '14.95';
$volume_discount = '0.3';

I know that comparing floating point numbers and doing calculations with floating point numbers can yield unexpected results, unless you use BC Math. But I’m wondering about just printing out the stored values as is. I tried echoing the float versions and they displayed as expected, but I’m wondering if that is reliable.

Note: I’ve used the term expected to explain what a layman would expect. As Eric points out, precise decimal floating point numbers is actually not the expected result.

EDIT: The code I am concerned with at the moment, checks the quantity and determines if the discount should be applied. Then does the following calculation:

$price -= round($price * $volume_discount, 2);

I normally use number_format to format the price for display, but I commented out the formatting for my testing. So far, regardless of price or discount values, PHP has been giving me the desired result. JavaScript, however, does not always give me the desired result. That is what led me to look at my PHP code again.

Obviously, I will adjust the JavaScript because that is where the error shows up, but I was trying to decide if I need to refactor my PHP code.

The numbers that illustrated the problem to me were 149.95 * 0.3. PHP displays 44.985 and JavaScript displays 44.98499999999999. So, even after rounding to two decimal places, I get different numbers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:53:44+00:00Added an answer on June 19, 2026 at 12:53 am

    Interpreting the decimal numeral “14.95” and storing it in a binary floating-point object requires a floating-point operation: Conversion from decimal to binary floating-point. Therefore, this is a floating-point operation and is subject to rounding error.

    That conversion might happen when the source text is interpreted by a compiler or interpreter or at run-time, but it does happen and is subject to rounding error.

    (Your question asserts that comparing and calculating with floating-point numbers yields unexpected results. This is true only if you do not expect the operations to obey the specification for those operations. You can avoid unexpected results by learning the specifications and therefore learning what to expect.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some prices in my DB which are stored as data type money
I have some product that has custom prices. Depending on the option selected, there
I need to load in some prices from a file (the server doesn't have
In the database, I have some of the products and prices. I need to
I have some historical option prices and I'm trying to determine an implied delta.
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I have a 'price' variable that contains some integer number from a MySQL database.
I have some data loaded as a np.ndarray and need to convert it to
I have some arbitrary pixel data that I want to save as a PNG.
I have some text lines like that : vt_wildshade2^508^508 vt_ailleurs2^1188^1188 ... vt_high2^13652^13652 Is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.