带有多个小数的数字的Javascript if语句
我正试图找出如何检测浏览器版本的网站支持的原因。我想知道浏览器是否比3.6.1更好,然后浏览器是否正常,否则显示错误 问题是我只能用小数点后1位来做这件事,一定有办法做到这一点 我尝试过带有多个小数的数字的Javascript if语句,javascript,math,browser,numbers,decimal,Javascript,Math,Browser,Numbers,Decimal,我正试图找出如何检测浏览器版本的网站支持的原因。我想知道浏览器是否比3.6.1更好,然后浏览器是否正常,否则显示错误 问题是我只能用小数点后1位来做这件事,一定有办法做到这一点 我尝试过parseFloat(“3.6.28”)但它只给出了3.6 我如何做到这一点: if(3.5.1 > 3.5.0) { //Pass! } 如果您将大量使用版本,那么编写这样的内容可能是值得的 function Version(str) { var arr = str.split('.');
parseFloat(“3.6.28”)
但它只给出了3.6
我如何做到这一点:
if(3.5.1 > 3.5.0)
{
//Pass!
}
如果您将大量使用版本,那么编写这样的内容可能是值得的
function Version(str) {
var arr = str.split('.');
this.major = +arr[0] || 0;
this.minor = +arr[1] || 0;
this.revision = +arr[2] || 0; // or whatever you want to call these
this.build = +arr[3] || 0; // just in case
this.toString();
}
Version.prototype.compare = function (anotherVersion) {
if (this.toString() === anotherVersion.toString())
return 0;
if (
this.major > anotherVersion.major ||
this.minor > anotherVersion.minor ||
this.revision > anotherVersion.revision ||
this.build > anotherVersion.build
) {
return 1;
}
return -1;
};
Version.prototype.toString = function () {
this.versionString = this.major + '.' + this.minor + '.' + this.revision;
if (this.build)
this.versionString += '.' + this.build;
return this.versionString;
};
现在
否则,只需使用所需的位3.5.1实际上不是一个数字,因此没有内置任何东西将其视为一个数字(例如,这就是parseFloat不起作用的原因)。我不确定javascript中是否存在任何类型的“版本”对象,但我的方法可能是进行拆分和分段比较
var a = new Version('3.5.1'),
b = new Version('3.5.0');
a.compare(b); // 1 , a is bigger than b
b.compare(a); // -1 , b is smaller than a
a.compare(a); // 0 , a is the same as a