big.js icon indicating copy to clipboard operation
big.js copied to clipboard

comperation between two numbers, getting wrong

Open dht20 opened this issue 1 year ago • 1 comments
trafficstars

i build a function `

function whoisbiggerxxx(a1, b1) {
	a = new Big(a1);
	b = new Big(b1);
	/* var a  = a1.split(".").pop().substring(0,10);
	var b  = b1.split(".").pop().substring(0,10);
	if ( first10Decimals  == second10Decimals )
	{
	   //they are equal
	} */
	if(a>b){
		console.log("a>b", a1+" > "+b1);
	}
	else if (a<b){
		console.log("a<b", a1+" < "+b1);
	}
	else if (a==b){
		console.log("a=b", a1+" = "+b1);
	}
}
	whoisbiggerxxx("1", "2");
	whoisbiggerxxx("1.1", "2");
	whoisbiggerxxx("110", "2.1");
	whoisbiggerxxx("1", "0.2");
	whoisbiggerxxx("4.64665558835226358211",  "464665558835226358.2");

` and my test result is

a<b 1 < 2 
a<b 1.1 < 2 
a<b 110 < 2.1 // wrong
a>b 1 > 0.2 
a<b 4.64665558835226358211 < 464665558835226358.2

dht20 avatar May 12 '24 08:05 dht20

Welcome to GitHub. Big doesn’t work with < or > since there is no way to do operator overloading with JavaScript. Take a look at the documentation which is linked off the readme. It explains how to do greater than and less than comparisons with Big.

nycdotnet avatar May 12 '24 18:05 nycdotnet