JavaScript string Numeric comparison problem

Source: Internet
Author: User

1. Background of the problem

Today, in the project encountered a problem: two input box, one input box value is 8.4, the other is 21.3, compare two input box value, there is 8.4 greater than 21.3


2, problem analysis

First, getting the value in the input box is a string, while the string comparison is based on the ASCII code, since 8 of the ASCII code is larger than the ASCII code of 2, resulting in 8.4 greater than 21.3


3, problem verification

(1) Design source code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

(2) Operation result



4. Solutions

(1) Converting a string into a numeric value

function Comparedata () {    var data1 = document.getElementById ("Inval"). Value;var data2 = document.getElementById (" Typev "). Value;if (number (DATA1) > Number (data2)) {    alert (data1+" greater than "+data2);} Else{alert (data1+ "less than equals" +data2);}}

(2) Operation result


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript string Numeric comparison problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.