?
Solved

php currency type

Posted on 2013-06-06
5
Medium Priority
?
162 Views
Last Modified: 2013-06-11
Hi

I have a variable which contains a price. This price is rendering as a string. How do I cast this to be a float or Int with 2 decimal places

I have tried the following...

 

$Gwith = "10";

$cart['total'] = number_format((float)$Gwith, 2, '.', '');

// output should be 10.00 as a 2 place decimal 

Open in new window


But this is rendering as a string?

I need this to be an Int as the gateway I am posting to requires it to be a numeric amount.
0
Comment
Question by:is_numeric
  • 3
5 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39224928
Int means integer and integers do not have decimal places.  If you have the number '10' you can use sprintf() to create a formatted version.  See Example #9: http://php.net/manual/en/function.sprintf.php
0
 
LVL 5

Expert Comment

by:magento
ID: 39224930
Not sure what you are asking...We are getting it as numeric only

<?php

$Gwith = "10";

$cart['total'] = number_format((float)$Gwith, 2, '.', '');

$int = $cart['total'];

print "Int value:".$int."\n";

if (is_numeric($int)) {
        echo "'{$int}' is numeric", PHP_EOL;
    } else {
        echo "'{$int}' is NOT numeric", PHP_EOL;
    }

?>

Open in new window

0
 

Author Comment

by:is_numeric
ID: 39224995
Basically my payment gateway expects this value NOT to be a string when posting so I need it to be of a currency type to 2 decimal places. Float i guess?

My use of the type 'Int' was wrong in this context in my original post. soz for any confusion (Soz means sorry)
0
 

Accepted Solution

by:
is_numeric earned 0 total points
ID: 39225071
Solved

Config issue in back office not allowing strings
0
 

Author Closing Comment

by:is_numeric
ID: 39237091
Mine was correct solution for this problem
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question