-2

I have a JSON object with order information in. Within this object is an amount for example "amount" : "82.95"

I have this within a variable called $amount and I am using it within an array by doing (int)$amount.

The problem with this is that it is removing the .95 which is required as it is an integer.

How can I get this value as a number and not a string not string but retain the .95?

Example:

$amount = 82.95;

echo (int)$amount;
// Result = 82
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ben H
  • 502
  • 1
  • 7
  • 24
  • 3
    You do realise what casting to an integer does to a decimal, don't you? Why do you want to cast it to an integer anyway if you want to retain the decimals? – Mark Baker Oct 12 '17 at 11:41
  • http://php.net/manual/en/language.types.string.php#language.types.string.conversion and look up the float data type http://php.net/manual/en/language.types.float.php – Nic3500 Oct 12 '17 at 11:43
  • Sorry, I didn't write the question very well, I am looking for an alternative method than integer. I will update that now. – Ben H Oct 12 '17 at 11:46
  • 1
    Possible duplicate of [PHP String to Float](https://stackoverflow.com/questions/481466/php-string-to-float) – mickmackusa Nov 27 '17 at 03:12

2 Answers2

0
<?php
    $amount = "82.95";
    echo (float)$amount;
?>
Nic3500
  • 8,144
  • 10
  • 29
  • 40
0

82.95 isn't an integer, so casting it as one isn't going to return 82.95. That type of number is a float:

$amount = '82.95';
echo (float) $amount;
// Result 82.95