Solved

php currency type

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

730 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