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!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.