php currency type


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.
Who is Participating?
is_numericConnect With a Mentor Author Commented:

Config issue in back office not allowing strings
Dave BaldwinFixer of ProblemsCommented:
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:
Not sure what you are asking...We are getting it as numeric only


$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

is_numericAuthor Commented:
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)
is_numericAuthor Commented:
Mine was correct solution for this problem
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.