Skip to content

Instantly share code, notes, and snippets.

@DQNEO
Created November 21, 2019 09:04
Show Gist options
  • Save DQNEO/4eae5db0cdf54139fdf6100de6909a5b to your computer and use it in GitHub Desktop.
Save DQNEO/4eae5db0cdf54139fdf6100de6909a5b to your computer and use it in GitHub Desktop.
PHP floating number
<?php
$my_json2 = '{"tax":2.03}';
$decoded = json_decode($my_json2, true);
echo $decoded['tax'] . PHP_EOL;
$tax_in_cents = round($decoded['tax'] * 100);
$response = ['tax_cents' => intval($tax_in_cents)];
echo json_encode($response) . PHP_EOL; // => 203
$tax_in_cents = $decoded['tax'] * 100;
$response = ['tax_cents' => intval($tax_in_cents)];
echo json_encode($response) . PHP_EOL; // => 202
@DQNEO
Copy link
Author

DQNEO commented Nov 21, 2019

Result

2.03
{"tax_cents":203}
{"tax_cents":202}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment