javascript - Converting -Infinity to Zero - Stack Overflow

I am working on some calculation and my calculation result is giving me NaN.So I used something like b

I am working on some calculation and my calculation result is giving me NaN. So I used something like below which converts it 0 which was fine

Number((((0-0)/0)*100).toFixed(2)) || 0

until the result is -Infinity and i found out that -Infinity is numeric value. So now i did something like below to convert -Infinity to 0

var result = Number((((0-18)/0)*100).toFixed(2)) || 0
if (result == Number.POSITIVE_INFINITY || result == Number.NEGATIVE_INFINITY)
{
    result=0;
	console.log(result)
}

I am working on some calculation and my calculation result is giving me NaN. So I used something like below which converts it 0 which was fine

Number((((0-0)/0)*100).toFixed(2)) || 0

until the result is -Infinity and i found out that -Infinity is numeric value. So now i did something like below to convert -Infinity to 0

var result = Number((((0-18)/0)*100).toFixed(2)) || 0
if (result == Number.POSITIVE_INFINITY || result == Number.NEGATIVE_INFINITY)
{
    result=0;
	console.log(result)
}

This is solving the problem but is there a shorter or better approach?

Share Improve this question edited Oct 12, 2019 at 16:14 Yves M. 31.1k24 gold badges109 silver badges149 bronze badges asked Oct 12, 2019 at 15:56 LearnerLearner 558 bronze badges 4
  • 2 Just don't divide by zero – Andreas Commented Oct 12, 2019 at 15:57
  • Agreed with @Andreas. Dividing by zero is generally useless. – Rojo Commented Oct 12, 2019 at 16:00
  • 1 Although dividing zero is useless, you can achieve this using ternary result = (Math.abs(Number((((0-18)/0)*100).toFixed(2))) == Infinity) ? 0 : Number((((0-18)/0)*100).toFixed(2)) || 0; – Daniel.V Commented Oct 12, 2019 at 16:02
  • @Daniel.V - which really isn't shorter :p – Bravo Commented Oct 12, 2019 at 16:05
Add a ment  | 

4 Answers 4

Reset to default 4

In Javascript exists isFinite() that returns if a number is finite or not. So:

var result = isFinite(Number((((0-18)/0)*100).toFixed(2))) || 0;
console.log(result);

The fastest way would be using bitwise Not operator:

var result = ~~Number((((0-18)/0)*100).toFixed(2))

Which executes a lot faster than other methods and is shorter to write.

This is solving the problem but is there a shorter or better approach?

The correct approach is to never divide by zero.

var a = 1;
var b = 2;
var c = 3;

var result;
if (c != 0) {
  result = ((a-b)*100)/c;
} else {
  // Zero is neither the correct result nor a good replacement for it
  // The correct approach here is to throw an exception
  result = 0;
}

I second the answer from @Riccardo Gai. Another approach could be to make use of the below 2 points in Javscript.

**Any number % Infinity  ===  number** 

**(Infinity || -Infinity) % Infinity ===  NaN**

So you can basically rewrite your code as...

Number((((0-18)/0)*100).toFixed(2)) % Infinity || 0

You can try out the below snippets..

console.log(Number((((0-0)/3)*100).toFixed(2)) % Infinity || 0);
console.log(Number((((0-1)/0)*100).toFixed(2)) % Infinity || 0);
console.log(Number((((1-0)/0)*100).toFixed(2)) % Infinity || 0);
console.log(Number((((1-2)/0)*100).toFixed(2)) % Infinity || 0);
console.log(Number((((1-2)/2)*100).toFixed(2)) % Infinity || 0);
console.log(Number((((0-18)/0)*100).toFixed(2)) % Infinity || 0);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745414565a4626685.html

相关推荐

  • javascript - Converting -Infinity to Zero - Stack Overflow

    I am working on some calculation and my calculation result is giving me NaN.So I used something like b

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信