Jump to content

Help properly converting a javascript function to php..


Scooby08

Recommended Posts

I have this function which is javascript and I need to convert it to php..

 

function US2dec(myUS) {
if (myUS.toLowerCase() == 'even') {
	myUS = '100';
}
var myDec;
myUS = parseFloat(myUS);
if (Math.abs(myUS) < 100 || isNaN(myUS)) {
	myDec = NaN;
} else if (myUS > 0) {
	myDec = 1 + myUS / 100;
} else {
	myDec = 1 - 100 / myUS;
}
return myDec.toFixed(;
}

 

I came up with this php version and it seems to work but I'm no expert with math functions.. I was hoping somebody who's a bit more familiar with these functions could check it over quick..

 

function US2dec($myUS) {
if (strtolower($myUS) == 'even') {
	$myUS = 100;
}
$myDec = 0;
$myUS = floatval($myUS);
if (abs($myUS) < 100 || !is_numeric($myUS)) {
	$myDec = 0;
} else if ($myUS > 0) {
	$myDec = 1 + $myUS / 100;
} else {
	$myDec = 1 - 100 / $myUS;
}
return number_format($myDec,;
}

 

Thanks!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.