Geo*_*ton 7 coldfusion version
我想确定一个版本号是否大于另一个版本号.版本号可以是以下任何一种:
4
4.2
4.22.2
4.2.2.233
...因为版本号超出了我的控制范围,所以我不能说数字中实际存在多少个点.
由于这个数字不是真正的数字,我不能简单地说,
Is 4.7 > 4.2.2
Run Code Online (Sandbox Code Playgroud)
如何将4.2.2之类的数字转换为可以根据其他版本号进行检查的实数?
我最好喜欢ColdFusion解决方案,但基本概念也没问题.
这是从Mango博客中的插件更新代码中删除的,并且稍微更新了一下.它应该完全符合你的要求.当参数1更大时返回1,当参数2更大时返回-1,当它们完全匹配时返回0.(注意4.0.1将与4.0.1.0完全匹配)
它使用CF列表函数,而不是数组,所以如果你切换到数组,你可能会看到一个小的性能提升......但是,嘿,它的工作原理!
function versionCompare( version1, version2 ){
var len1 = listLen(arguments.version1, '.');
var len2 = listLen(arguments.version2, '.');
var i = 0;
var piece1 = '';
var piece2 = '';
if (len1 gt len2){
arguments.version2 = arguments.version2 & repeatString('.0', len1-len2);
}else if (len2 gt len1){
arguments.version1 = arguments.version1 & repeatString('.0', len2-len1);
}
for (i=1; i lte listLen(arguments.version1, '.'); i=i+1){
piece1 = listGetAt(arguments.version1, i, '.');
piece2 = listGetAt(arguments.version2, i, '.');
if (piece1 neq piece2){
if (piece1 gt piece2){
return 1;
}else{
return -1;
}
}
}
//equal
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行示例测试:
<cfoutput>#versionCompare('4.7', '4.2.2')#</cfoutput>
Run Code Online (Sandbox Code Playgroud)
打印:
1
| 归档时间: |
|
| 查看次数: |
647 次 |
| 最近记录: |