Solved

php currency type

Posted on 2013-06-06
5
152 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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php date - compare 4 40
PHP 7 with IIs and SQL Server Windows 2008 Server failed to connect 8 54
Access Object's Property 9 22
How do I put a session for a radio button? 4 13
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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 …

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now