-1

I want to convert this php code snippet that converts/truncates a value:

here is the php code:

$currentcost = 42.35;
$currentcost = explode(".", $currentcost);

if($currentcost[1] <= 35) {
$currentcost[1] = 99;
$currentcost[0] -= 1;
}
echo $output = $currentcost[0] . "." . $currentcost[1]; 

This is supposed to be the condition:

x.35 where x is any number. If the decimal number is less than or equal to 35 then convert the .35 into 99 and subtract the x value with 1.

Something like this:

42.35 will become 41.99

Bakudan
  • 19,134
  • 9
  • 53
  • 73
jovazel
  • 79
  • 3
  • 7

2 Answers2

2

If you want to port it to Javascript as is (without the % optimization as shown in the other answer here), then have a look at the array.split method documentation. It should work like this:

var currentcost = 42.35;
var costpart = String(currentcost).split(".");
if (costpart[1] <= 35) {
    costpart[1] = 99;
    costpart[0] -= 1;
}
var output = costpart[0] + "." + costpart[1];
document.write(output);
codeling
  • 11,056
  • 4
  • 42
  • 71
1

Here you go, it uses the modulo operator to get the decimal part of the number:

var currentcost = 42.35;

if ( (currentcost*100) % 100 <= 35) {
  currentcost = Math.floor(currentcost) - 0.01;
}

document.write(currentcost);

Edit: For an explanation on the way floating point values are handled here, see this question.

Community
  • 1
  • 1
RageZ
  • 26,800
  • 12
  • 67
  • 76
  • Hm, that doesn't seem to work (see http://jsfiddle.net/L9ksC/); first, it should be `<=`, instead of `<`, and second, it seems the numerical accuracy in javascript prevents `42.35 % 1 <= 0.35` from being true... – codeling Dec 23 '11 at 13:14
  • the new solution is even worse - Math.ceil returns the nearest **integer**, meaning the part before % 1 in the if condition will always be 0 (which is smaller then 0.35), meaning it will always return *next smaller integer, with .99 after comma* ... and I think %1 doesn't work as you think it does (and not as I thought at first too). – codeling Dec 23 '11 at 13:28
  • @nyarlathotep: yeah that was wrong, I don't think I can get away from the floating point error, appart doing something silly like `<= 0.350000000014` or <= 0.36 – RageZ Dec 23 '11 at 13:33
  • true, floating point variables are always tricky ;). For ideas on how to get it work (would be a nicer solution after all: http://stackoverflow.com/questions/3966484/floating-point-numbers-and-javascript-modulus-operator) Multiplying before modulo seems to do the trick, as you tried, but without the division and without Math.ceil... – codeling Dec 23 '11 at 13:38
  • actually < 0.36 make the trick if you are manipulating money ;-) – RageZ Dec 23 '11 at 13:40
  • Yeah but that'll only work if you always only use 2 digits after the comma; values like 42.351 would also be handled. Check out my solution (see link in my last comment): multiplying by 100 before modulo... that way, it also works if there should be more than 2 digits after comma in the given value! I took the liberty of editing your answer, hope that's ok. – codeling Dec 23 '11 at 13:45