Linux shell implementation to determine if the number entered is a reasonable floating-point _linux shell

Source: Internet
Author: User

This shell is to determine if the number entered is a reasonable float

The implementation code is as follows:

#!/bin/sh # Validfloat-Tests Whether a number is a valid floating-point value.

# This script cannot accept scientific (1.304E5) notation. # to test whether ' a entered value is a valid floating-point number, we ' need to split the ' value at the ' decimal point. We then test the "I" # to "if it" s a valid integer, then test the second part to the if it's a # valid >=0 in

Teger, so-30.5 is valid, but-30.-8 isn ' t. . Validint # Bourne Shell notation to source the Validint function validfloat () {fvalue= ' $ ' if [!-Z $ (echo $fvalue | Sed ' s/[^.] G ')]; Then decimalpart= "$ (echo $fvalue | cut-d.-f1)" fractionalpart= "$ (echo $fvalue | cut-d.-f2)" If [!-Z $decimal Part]; Then if! Validint "$decimalPart" "" "" "; Then return 1 fi fi if ["${fractionalpart%${fractionalpart#?}}" = "-"];  Then echo "Invalid floating-point number: '-' not allowed \ on decimal point ' >&2 return 1 fi if [ "$fractionalPart" "!="]; Then if! Validint "$fractionalPart" "0" ""; Then return 1 fi fi if ["$decimalPart" = "-"-o-z "$decimalPart"]; Then if [-Z $fractionalPart]; Then echo "Invalid floating-point format." >&2; Return 1 fi fi else if ["$fvalue" = "-"]; Then echo "Invalid floating-point format." >&2; Return 1 fi if! Validint "$fvalue" "" "" "; Then return 1 fi fi return 0}

Notice:
1: if [!-Z $ (echo $fvalue | sed ' s/[^.] G ')] will be entered to. divide into integers and decimal parts.
2): If ["${fractionalpart%${fractionalpart#?}}" = "-"] to determine if the "-" number after the decimal point, the output character is not valid
3) Then some if statement is to judge the decimal and integer parts of the law
4) Because the Valiint function is not given, the script cannot be fully executed, and the Valiint function is to determine whether the string is all numeric.

Related Article

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.