Javascript compare numbers as strings

前端 未结 2 1000
醉话见心
醉话见心 2021-01-23 09:04

I wish to compare two strings in javascript. I am using localeCompare method but the output is not as expected

116457 < 3085
false

\"116457\" < \"3085\"
         


        
2条回答
  •  难免孤独
    2021-01-23 09:42

    If you want to compare them without converting them to numbers, you can set numeric: true in the options parameter

    console.log(
      "116457".localeCompare("3085", undefined, { numeric: true })
    )
    console.log(
      "116457".localeCompare("3085")
    )

提交回复
热议问题