Solved

php currency type

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
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.

759 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

20 Experts available now in Live!

Get 1:1 Help Now