Joseph
Joseph

Reputation: 4725

What's the magic of JavaScript multiply by 1?

I've encountered this code:

const currency = "TWD";

function priceMathCeil(_int) {
  if (currency === "TWD") {
    return _int * 1;
  } else {
    return _int;
  }
}

I know this code looks useless, but JavaScript has so many pitfalls like 0.1 + 0.2 = 0.30000000000000004 something like that.

So I was wondering if I'm missing anything about multiply by 1 in JavaScript?

Upvotes: 0

Views: 502

Answers (1)

Brad
Brad

Reputation: 163488

That's not a pitfall... that's how computers work. Look into floating point math.

As for the * 1, I suspect it's leftover from a previous currency rate change. The only other thing would be forcing the type by using this operator, but I can't see why someone would do that only in the case of a certain currency.

Upvotes: 4

Related Questions