最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - comparing two decimal values with typescript - Stack Overflow

programmeradmin1浏览0评论

I'm trying to pop up a message to the user when one value is bigger than the other with javascript. I've used float but when the scripts should show true it doesn't show up

appVersionAndroid = '1.9'
appVersionWebAndroid = '1.10'

if(parseFloat(this.appVersionAndroid) < parseFloat(this.appVersionWebAndroid)) {
  alert('needs update')
}

I'm trying to pop up a message to the user when one value is bigger than the other with javascript. I've used float but when the scripts should show true it doesn't show up

appVersionAndroid = '1.9'
appVersionWebAndroid = '1.10'

if(parseFloat(this.appVersionAndroid) < parseFloat(this.appVersionWebAndroid)) {
  alert('needs update')
}
Share Improve this question edited Jun 20, 2018 at 9:37 coudy.one 1,43013 silver badges24 bronze badges asked Jun 20, 2018 at 9:21 user6579134user6579134 8393 gold badges12 silver badges36 bronze badges 3
  • Are you sure your condition is correct? – Ankit Agarwal Commented Jun 20, 2018 at 9:25
  • This won't work. Instead, split the strings by dot and pare each number in the array, starting from left. – Farooq AR Commented Jun 20, 2018 at 9:26
  • appVersionAndroid is 1.9 which is the same as 1.90. appVersionWebAndroid is 1.10. 1.90 > 1.10 – Alex Commented Jun 20, 2018 at 9:41
Add a ment  | 

3 Answers 3

Reset to default 4

You're code is ok and there is no issue. There is a small mistake where you put the values. you put the appVersionAndroid value "1.9" and appVersionWebAndroid value 1.10, here in actually the first value is greater then second because float converter have two decimal number, I means 1.9 means 1.90 here which is greater then 1.10. So thats why your condition go to false.

Sorry for my english. But I think you understand what i want to say.

appVersionAndroid = "1.10"
appVersionWebAndroid = "1.90"
 if(parseFloat(this.appVersionAndroid) < parseFloat(this.appVersionWebAndroid)){
alert('needs update')
}

1.9 is bigger than 1.10. That's why alert doesn't show up.

the number 1.1 (which is the same as 1.10) is not greater that 1.9

You could for instance use 2 different variables for the version before and after the dots.

The main idea is to treat the number before and after the dot as two different integers numbers (for instance you can call them major and minor version), and it's not at all the same as having a big float number.

Then implement a parison function which would go like this :

First pare the major number. If it's equal, pare the minor number.

发布评论

评论列表(0)

  1. 暂无评论