Forum Moderators: coopster

Message Too Old, No Replies

number format() string or real?

         

SeanF

10:48 am on Mar 24, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



I have a form that passes a value to a credit card processing gateway. The form takes the user's input does a couple of calculations to determine a processing fee and then sends the calculated amount along with card information to the gateway.

The calculations sometime result in a number with more than two digits to the right of the decimal point. Previously, this has not been a problem as the gateway trimmed to two digits. (sloppy, I know). The gateway seems to have changed and now rejects numbers with more than 2 digits to the right of the decimal.

My question is: Does number_format() return a string or a real number? If I use $totalVal = number_format($someVal, 2) and pass that to the gateway, will it receive a real number as it is expecting?

Is there a better way to put the calculated number into a correct currency format?

Thanks

robzilla

11:53 am on Mar 24, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



var_dump(number_format(89.23892, 2));
> string(5) "89.24"

Whether it's a string or a float in your script is irrelevant to the gateway, because your output (the value passed to the gateway) could be the same.

However, number_format() groups thousands and your gateway probably prefers (or requires) a real number like 9999.99 over 9,999.99.

The "correct currency format" is, presumably, the number format that the gateway expects, regardless of currency.

So, instead, you could simply use round().
var_dump(round(100089.23892, 2));
> float(100089.24)

SeanF

12:30 pm on Mar 24, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thanks!

Yes, after posting my query, it occurred to me that the problem may be the comma which number_format() uses. Kind of ugly, but I used str_replace() to remove the comma.

I will go back and replace with round()... much cleaner.

Thanks again