Click here to Skip to main content
15,888,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey. Stumped over here.

I have a JSON string returning data from a payment API, it is implemented into my html code via PHP:

The issue is that the API pulls in number values only with no decimal conversion on the API end. My system is converting the number to a decimal which is one decimal place off from where it should be.

For example:
Returned from API: Total: 500 (Not converted json value)
Total: $500.00 (What I see)
Total: $5.00 (What I want)

Here is the snippet of code that I am using in my HTML file to show the data.

PHP
<?php echo ((isset($processData['original_total_charged']) && !empty($processData['original_total_charged'])) ? $this->Number->currency($processData['original_total_charged'], Configure::read('PAYMENT_SETTINGS.Currency')): ""); ?>


"original_total_charged" being the id of the number string being returned.

Where is my issue? I feel that I understand what the issue is; but I don't know WHERE I've made my mistake. My system is built with mvc and cake php
Posted
Updated 16-Dec-15 14:09pm
v3
Comments
Peter_in_2780 16-Dec-15 20:38pm    
Looks like the API returns integer cents, which is a very sensible representation of monetary value. Look around your frameworks for a currency class that includes formatting, so it will produce strings like "$5.00" or "$0.50" or "50c".
zakpucci2 16-Dec-15 20:39pm    
That's what I assumed, I'm just having the hardest time finding it.

1 solution

PHP JSON serialization does it all. Please see: http://php.net/manual/en/function.json-decode.php[^].

Essentially, "decode" (parsing) reinterprets a JSON string into a PHP data structure in type-agnostic data-driven way, as it is always done with true serialization.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900