?
Solved

php currency type

Posted on 2013-06-06
5
Medium Priority
?
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 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