0

I have the following code which multiplies a number (number is stored in a database) and I then download the result to a CSV file. The resulting number is given as a whole number rather than including a decimal place.

Current Code

<? return ((float) $amount*1) ?>

Current Result: 2 x 2.13 = 4

Desired Result: 2 x 2.13 = 4.26

In this example $amount = 2.13

Can anyone please suggest an edit to get this working?

YorkieMagento
  • 336
  • 2
  • 9
  • 25
  • How do you use that returned result? Can you share some more of the relevant code, the function (I assume since you `return`?) and how you use it? – Qirel Apr 23 '17 at 13:47
  • Sounds similar - http://stackoverflow.com/questions/19875583/set-precision-for-a-float-number-in-php – DaveStSomeWhere Apr 23 '17 at 13:50
  • Did you open it with a text editor or some spreadsheet application ? The formating of the app might have something to do with it – frz3993 Apr 23 '17 at 13:52
  • This question makes no sense, really... – FirstOne Apr 23 '17 at 13:54
  • frz3993 I know it's not formatting as other values I pull from db that don't have a calculation show the decimal. FirstOne I am multiplying a decimal and receive a result that is not a decimal, as per above example. – YorkieMagento Apr 23 '17 at 14:13

1 Answers1

1

You can use number_format for that:

number_format(2.13 * 2, 2); // "4.26"

See https://3v4l.org/GlARW for performance related information.

Tom Udding
  • 2,264
  • 3
  • 20
  • 30
  • If its a float, you can just echo the result directly, unless you want a specific amount of decimals - the `number_format()` isn't needed. https://3v4l.org/QOp91 – Qirel Apr 23 '17 at 13:55