String Compare “Logic”

前端 未结 11 1635
悲哀的现实
悲哀的现实 2020-12-06 19:06

Can anybody please tell me why the string comparisons below deliver these results?

>>\"1040\"<=\"12000\"  
True  
>> \"1040\"<=\"1         


        
11条回答
  •  甜味超标
    2020-12-06 19:37

    In C, string comparisons are done character by character. In the first case, the first characters of the stings are equal, so it comes down to the second character: '0' is < '2', so "1040" < "12000". In the second case, the first two characters of the strings are equal, so the third character is the basis -- '4' > '0', so "1040" > "10000".

    If you want them compared as numbers, you'll need to convert them to numbers first, then do the comparison.

提交回复
热议问题