SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: integer to float
-
Aug 24, 2007, 12:09 #1
- Join Date
- Aug 2006
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
integer to float
Hi,
How can I change an integer value to float with 2 digits after the decimal places, in php.
for example:
i need to change 100 to be 1.00
thanks
-
Aug 24, 2007, 12:15 #2
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
So, what about 123 - would that be 1.23? If so:
PHP Code:$number = number_format($number / 100, 2);
-
Aug 24, 2007, 12:17 #3
- Join Date
- May 2006
- Location
- Ventura, CA
- Posts
- 2,750
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I must ask why this sort of formatting is required. If it needs to be 1.00 then why isn't it received as such?
Last edited by chris_fuel; Aug 24, 2007 at 12:18. Reason: Posted too slow :S
-
Aug 24, 2007, 12:43 #4
- Join Date
- Aug 2006
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've tried your code and customised it but it gives me 0.00 rather than 1.00
PHP Code:$number = number_format($number / $_POST['PurchaseAmount'], 2);
Thanks
-
Aug 24, 2007, 13:01 #5
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
That's because $number is supposed to be equal to $_POST['PurchaseAmount']. Also, you should check ("sanitize") that PurchaseAmount is a reasonable number, and a number at all.
PHP Code:$pa = $_POST['PurchaseAmount'];
if (!is_numeric($pa)) {
//error
}
else {
$pa = number_format($pa / 100, 2);
}
-
Aug 24, 2007, 13:15 #6
- Join Date
- Aug 2006
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
aha, i understand
This works now.
Many thanks
Bookmarks