Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## Script originally converted from batch script
- ## There is no warranty on this script or it's results
- ## Provided as-is bv1.2
- ################################################################################
- ################################### Functions ##################################
- ################################################################################
- function isgreater
- {
- local _GTR_NUM=($1 $2)
- for i in 0 1; do
- while [[ "${_GTR_NUM[$i]:0:1}" == "0" ]]; do
- _GTR_NUM[$i]=${_GTR_NUM[$i]:1}
- done
- if [[ -z "${_GTR_NUM[$i]}" ]]; then
- _GTR_NUM[$i]=0
- fi
- done
- if [[ ${#_GTR_NUM[0]} -gt ${#_GTR_NUM[1]} ]]; then
- return 1
- elif [[ ${#_GTR_NUM[1]} -gt ${#_GTR_NUM[0]} ]]; then
- return 3
- else
- for ((j=0; j<${#_GTR_NUM[0]}; j++)); do
- if [[ ${_GTR_NUM[0]:j:1} -gt ${_GTR_NUM[1]:j:1} ]]; then
- return 1
- fi
- if [[ ${_GTR_NUM[1]:j:1} -gt ${_GTR_NUM[0]:j:1} ]]; then
- return 3
- fi
- done
- fi
- return 2
- }
- function str_add
- {
- local _ADD_NUM=($1 $2)
- local _ADD_PAD=0
- local _ADD_CAR=0
- local _ADD_TOT=""
- local _ADD_LEN=(${#_ADD_NUM[0]} ${#_ADD_NUM[1]})
- local a=0
- if [[ ${_ADD_LEN[0]} -gt ${_ADD_LEN[1]} ]]; then
- _ADD_PAD=$((_ADD_LEN[0] - 1))
- else
- _ADD_PAD=$((_ADD_LEN[1] - 1))
- fi
- for ((a=_ADD_LEN[0]; a<=_ADD_PAD ; a++)); do
- _ADD_NUM[0]=0${_ADD_NUM[0]}
- done
- for ((a=_ADD_LEN[1]; a<=_ADD_PAD ; a++)); do
- _ADD_NUM[1]=0${_ADD_NUM[1]}
- done
- for ((a=_ADD_PAD; a>=0 ; a--)); do
- _ADD_CAR=$((_ADD_CAR + ${_ADD_NUM[0]:$a:1} + ${_ADD_NUM[1]:$a:1}))
- _ADD_TOT=${_ADD_CAR:(-1)}$_ADD_TOT
- _ADD_CAR=${_ADD_CAR:0:(-1)}
- if [[ -z "$_ADD_CAR" ]]; then
- _ADD_CAR=0
- fi
- done
- if [[ $_ADD_CAR -gt 0 ]]; then
- _ADD_TOT=$_ADD_CAR$_ADD_TOT
- fi
- echo $_ADD_TOT
- }
- function str_sub
- {
- local _SUB_NUM=($1 $2)
- local _SUB_CHA=""
- local _SUB_RES=""
- local _SUB_TAKE=""
- local _SUB_PAD=""
- local _SUB_TOT=""
- local _SUB_LEN=(${#_SUB_NUM[0]} ${#_SUB_NUM[1]})
- if [[ ${_SUB_LEN[0]} -gt ${_SUB_LEN[1]} ]]; then
- _SUB_PAD=$((_SUB_LEN[0] - 1))
- else
- _SUB_PAD=$((_SUB_LEN[1] - 1))
- fi
- for ((a=_SUB_LEN[0]; a<=_SUB_PAD ; a++)); do
- _SUB_NUM[0]=0${_SUB_NUM[0]}
- done
- for ((a=_SUB_LEN[1]; a<=_SUB_PAD ; a++)); do
- _SUB_NUM[1]=0${_SUB_NUM[1]}
- done
- for ((a=_SUB_PAD; a>=0 ; a--)); do
- _SUB_RES=$(( ${_SUB_NUM[0]:$a:1} - ${_SUB_NUM[1]:$a:1} ))
- if [[ $_SUB_RES -lt 0 ]]; then
- _SUB_TAKE=
- for ((b=a; b>=0; b--)); do
- if [[ $a -ne $b ]]; then
- if [[ -z "$_SUB_TAKE" ]]; then
- if [[ "${_SUB_NUM[0]:$b:1}" != "0" ]]; then
- _SUB_CHA=$((${_SUB_NUM[0]:$b:1} - 1))
- _SUB_TAKE=$((b + 1))
- _SUB_NUM[0]=${_SUB_NUM[0]:0:$b}$_SUB_CHA${_SUB_NUM[0]:$_SUB_TAKE}
- _SUB_RES=$(( 1${_SUB_NUM[0]:$a:1} - ${_SUB_NUM[1]:$a:1} ))
- else
- _SUB_CHA=$((b + 1))
- _SUB_NUM[0]=${_SUB_NUM[0]:0:$b}9${_SUB_NUM[0]:$_SUB_CHA}
- fi
- fi
- fi
- done
- fi
- _SUB_TOT=$_SUB_RES$_SUB_TOT
- done
- while [[ "${_SUB_TOT:0:1}" == "0" ]]; do
- _SUB_TOT=${_SUB_TOT:1}
- done
- if [[ -z $_SUB_TOT ]]; then
- _SUB_TOT=0
- fi
- echo $_SUB_TOT
- }
- function str_mult
- {
- local _MUL_NUM=($1 $2)
- local _MUL_CAR=0
- local _MUL_TOT=0
- local _MUL_ROW=""
- local _MUL_X10=""
- local _MUL_REV=("" "")
- for a in 0 1; do
- for ((b=0; b<${#_MUL_NUM[$a]}; b++)); do
- _MUL_REV[$a]=${_MUL_NUM[$a]:$b:1}\ ${_MUL_REV[$a]}
- done
- done
- for a in ${_MUL_REV[0]}; do
- _MUL_ROW=$_MUL_X10
- for b in ${_MUL_REV[1]}; do
- _MUL_CAR=$(( (a * b) + _MUL_CAR ))
- _MUL_ROW=${_MUL_CAR:(-1)}$_MUL_ROW
- _MUL_CAR=${_MUL_CAR:0:(-1)}
- if [[ -z "$_MUL_CAR" ]]; then
- _MUL_CAR=0
- fi
- done
- if [[ -n "${_MUL_CAR//0/}${_MUL_ROW//0/}" ]]; then
- #may need to remove leading zeros
- _MUL_TOT=$(str_add $_MUL_CAR$_MUL_ROW $_MUL_TOT)
- fi
- _MUL_CAR=0
- _MUL_X10=0$_MUL_X10
- done
- echo "$_MUL_TOT"
- }
- function str_div
- {
- local _DIV_PAD=""
- local _DIV_X10=""
- local _DIV_DONE=0
- local _DIV_TOT=0
- if [[ -z "$4" ]]; then
- for ((a=1; a<=$3; a++)); do
- _DIV_PAD=0$_DIV_PAD
- done
- fi
- local _DIV_NUM=($1$_DIV_PAD $2)
- while [[ "${_DIV_NUM[0]:0:1}" == "0" ]]; do
- _DIV_NUM[0]=${_DIV_NUM[0]:1}
- done
- while [[ "${_DIV_NUM[1]:0:1}" == "0" ]]; do
- _DIV_NUM[1]=${_DIV_NUM[1]:1}
- done
- for ((a=${#_DIV_NUM[1]}; a<${#_DIV_NUM[0]}; a++)); do
- _DIV_X10=0$_DIV_X10
- done
- while [[ "$_DIV_DONE" == "0" ]]; do
- isgreater "${_DIV_NUM[0]}" "${_DIV_NUM[1]}$_DIV_X10"
- if [[ $? -le 2 ]]; then
- _DIV_NUM[0]=$(str_sub "${_DIV_NUM[0]}" "${_DIV_NUM[1]}$_DIV_X10")
- _DIV_TOT=$(str_add $_DIV_TOT 1$_DIV_X10)
- elif [[ -n "$_DIV_X10" ]]; then
- _DIV_X10=${_DIV_X10:1}
- else
- _DIV_DONE=1
- fi
- done
- if [[ -n "$4" ]]; then
- _DIV_TOT=${_DIV_NUM[0]}
- fi
- echo "$_DIV_TOT"
- }
- function str_exp
- {
- local _EXP_SRL=""
- local _EXP_SQU=$2
- local _EXP_TOT=$1
- local _EXP_PRC=1
- while [[ $_EXP_PRC -eq 1 ]]; do
- _EXP_SQU+="0"
- _EXP_SQU=$(str_div "$_EXP_SQU" "2" "0")
- if [[ "${_EXP_SQU:(-1)}" == "5" ]]; then
- _EXP_SRL+=" $_EXP_TOT"
- fi
- _EXP_SQU=${_EXP_SQU:0:(-1)}
- if [[ -z "$_EXP_SQU" ]]; then
- _EXP_PRC=0
- else
- _EXP_TOT=$(str_mult "$_EXP_TOT" "$_EXP_TOT")
- if [[ "$_EXP_SQU" == "3" ]]; then
- _EXP_SRL+=" $_EXP_TOT"
- _EXP_SQU=2
- fi
- if [[ "$_EXP_SQU" == "2" ]]; then
- _EXP_TOT=$(str_mult "$_EXP_TOT" "$_EXP_TOT")
- for remaining in $_EXP_SRL; do
- _EXP_TOT=$(str_mult "$_EXP_TOT" "$remaining")
- done
- _EXP_PRC=0
- fi
- if [[ "$_EXP_SQU" == "1" ]]; then
- _EXP_PRC=0
- fi
- fi
- done
- echo "$_EXP_TOT"
- }
- ################################################################################
- ################################## Main script #################################
- ################################################################################
- if [[ -z "$3" ]]; then
- echo
- echo "STR_MATH BASH version bv1.2"
- echo
- echo " Large number and floating point work around script for Addition,"
- echo " Subtraction, Division, Multiplaction, Modulus and exponents."
- echo " Floating point and negative numbers supported."
- echo
- echo " Usage:"
- echo " STR_MATH {Number} {+-XM} {Number}"
- echo " STR_MATH {Number} {/Y} {Number} [n max decimal places]"
- echo
- echo " Division and certain negative exponents default to a maximum"
- echo " of ten decimal places of output. An optional user defined max"
- echo " can be input as the fourth number when applicable."
- echo
- echo " No rounding is performed on output of division, the number"
- echo " is only truncated. For example a result of 0.1257 with three"
- echo " places will return 0.125 NOT 0.126."
- echo
- echo " Square brackets can be used on negative base exponents instead"
- echo " of parentheses if needed:"
- echo " STR_MATH [-5] Y 10"
- echo " STR_MATH -5 Y 10"
- echo
- echo " Fractional exponents are not supported."
- echo
- echo " Results are not garanteed. This script is AS-IS."
- echo
- echo " Output Will be echo'd"
- echo
- echo "Judago 2016"
- echo
- exit
- fi
- _VAR_TMP=""
- _INP_NEG=("" "")
- _INP_DP=(0 0)
- _OUT_DP=""
- _OUT_NEG=
- _INP_NUM=("$1" "$3")
- _INP_OP="$2"
- _INP_EXP_PAR=0
- _INP_EXP_DIV=0
- #Default number of decimal places for division
- _DIV_DP=10
- ################################################################################
- ############################### Input validation ###############################
- ################################################################################
- ###### Validate the operator.
- if [[ "$_INP_OP" == "*" ]]; then
- _INP_OP=X
- fi
- if [[ "$_INP_OP" == "%" ]]; then
- _INP_OP=M
- fi
- _INP_OP=${_INP_OP//y/Y}
- _INP_OP=${_INP_OP//m/M}
- _INP_OP=${_INP_OP//x/X}
- if [[ ${#_INP_OP} -ne 1 || -n "${_INP_OP//[\/X+MY\-]/}" ]]; then
- 1>&2 echo "ERROR: Bad operator: " "$*"
- exit
- fi
- ###### Only 3 arguments should be available, except for the optional decimal
- ###### places for division, where 4 can be used.
- if [[ -n "${_INP_OP//[\/Y]/}" && -n "$4" || -z "${_INP_OP//[\/Y]}" && -n "$5" ]]; then
- 1>&2 echo "ERROR: Too many arguments: " "$*"
- exit
- fi
- ###### Fractional exponents aren't supported
- if [[ "${_INP_OP}" == "Y" && "${_INP_NUM[0]//./}${_INP_NUM[1]//./}" != "${_INP_NUM[0]}${_INP_NUM[1]}" ]]; then
- 1>&2 echo "ERROR: Fractional exponents are not supported"
- exit
- fi
- ###### Test for paranthisis on input negative exponent
- if [[ "${_INP_OP}" == "Y" && -n "${_INP_NUM[0]}" ]]; then
- if [[ "${_INP_NUM[0]//[\[\]]/}" != "${_INP_NUM[0]}" ]]; then
- _INP_EXP_PAR=1
- _INP_NUM[0]="${_INP_NUM[0]//[\[\]]/}"
- fi
- fi
- ###### Validate input numbers
- for i in 0 1; do
- if [[ ! "${_INP_NUM[$i]}" =~ ^[0-9]*$
- && ! "${_INP_NUM[$i]}" =~ ^-[0-9]*$
- && ! "${_INP_NUM[$i]}" =~ ^[0-9]*\.[0-9]*$
- && ! "${_INP_NUM[$i]}" =~ ^-[0-9]*\.[0-9]*$
- ]]
- then
- 1>&2 echo "ERROR: Invalid input: " "$*"
- exit
- fi
- #negative
- if [[ "${_INP_NUM[$i]//-/}" != "${_INP_NUM[$i]}" ]]; then
- if [[ ${#_INP_NUM[$i]} -eq 1 ]]; then
- 1>&2 echo "ERROR: Invalid input: " "$*"
- exit
- else
- _INP_NUM[$i]=${_INP_NUM[$i]:1}
- _INP_NEG[$i]=-
- fi
- fi
- #decimal point
- if [[ "${_INP_NUM[$i]//\./}" != "${_INP_NUM[$i]}" ]]; then
- if [[ ${#_INP_NUM[$i]} -eq 1 ]]; then
- 1>&2 echo "ERROR: Invalid input: " "$*"
- exit
- fi
- fi
- done
- ################################################################################
- ################################ Input parsing #################################
- ################################################################################
- ###### Change number of decimal places for division/negative exponents if requested. Strip leading zeros, if any.
- if [[ -z "${_INP_OP//[\/Y]/}" && -n "$4" ]]; then
- if [[ -z "${4//[0-9]/}" ]]; then
- _DIV_DP="$4"
- while [[ "${_DIV_DP:0:1}" == "0" ]]; do
- _DIV_DP=${_DIV_DP:1}
- done
- if [[ -z $_DIV_DP ]]; then
- _DIV_DP=0
- fi
- else
- 1>&2 echo "ERROR: Invalid input: " "$*"
- exit
- fi
- fi
- ###### Remove leading zero, return for sums including zero and get position of decimal places
- for i in 0 1 ; do
- #Remove leading zeros
- while [ "${_INP_NUM[$i]:0:1}" == "0" ]; do
- _INP_NUM[$i]=${_INP_NUM[$i]:1}
- done
- #Ensure the input is populated and has a leading and trailing zero for floating point.
- if [[ -z "${_INP_NUM[$i]}" ]]; then
- _INP_NUM[$i]=0
- fi
- if [[ "${_INP_NUM[$i]:0:1}" == "." ]]; then
- _INP_NUM[$i]=0${_INP_NUM[$i]}
- fi
- if [[ "${_INP_NUM[$i]:-1}" == "." ]]; then
- _INP_NUM[$i]=${_INP_NUM[$i]}0
- fi
- # Return for zero sums
- if [[ "${_INP_NUM[$i]}" == "0" ]]; then
- if [[ "$_INP_OP" == "X" ]]; then
- echo "0"
- elif [[ "$_INP_OP" == "/" || "$_INP_OP" == "M" ]]; then
- if [[ $i -eq 1 ]]; then
- 1>&2 echo "ERROR: Divide by zero"
- else
- if [[ ${_INP_NUM[1]} -eq 0 ]]; then
- 1>&2 echo "ERROR: Divide by zero"
- exit
- fi
- echo "0"
- fi
- elif [[ "$_INP_OP" == "-" ]]; then
- if [[ $i -eq 0 ]]; then
- if [[ -n ${_INP_NEG[1]} ]]; then
- echo ${_INP_NUM[1]}
- else
- echo -${_INP_NUM[1]}
- fi
- else
- echo ${_INP_NEG[0]}${_INP_NUM[0]}
- fi
- elif [[ "$_INP_OP" == "+" ]]; then
- if [[ $i -eq 0 ]]; then
- echo ${_INP_NEG[1]}${_INP_NUM[1]}
- else
- echo ${_INP_NEG[0]}${_INP_NUM[0]}
- fi
- elif [[ "$_INP_OP" == "Y" ]]; then
- if [[ $i -eq 0 ]]; then
- if [[ -n "${_INP_NEG[1]}" ]]; then
- 1>&2 echo "ERROR: Negative exponents of zero are undefined."
- else
- echo 0
- fi
- else
- if [[ $_INP_EXP_PAR -eq 1 ]]; then
- echo 1
- else
- echo "${_INP_NEG[0]}1"
- fi
- fi
- fi
- exit
- fi
- #get position of decimal point
- if [[ "${_INP_NUM[$i]//./}" != "${_INP_NUM[$i]}" ]]; then
- for ((j=${#_INP_NUM[$i]} - 1; j>=0; j--)); do
- if [[ "${_INP_NUM[$i]:$j:1}" == "." ]]; then
- _INP_DP[$i]=$((${#_INP_NUM[$i]} - (j + 1)))
- fi
- done
- fi
- done
- ###### Floating point normalisation
- _INP_NUM[0]=${_INP_NUM[0]//./}
- _INP_NUM[1]=${_INP_NUM[1]//./}
- if [[ ${_INP_DP[0]} -gt ${_INP_DP[1]} ]]; then
- _OUT_DP=$((_INP_DP[0] - 1))
- else
- _OUT_DP=$((_INP_DP[1] - 1))
- fi
- for ((i=_INP_DP[0]; i<=_OUT_DP; i++)); do
- _INP_NUM[0]=${_INP_NUM[0]}0
- done
- for ((i=_INP_DP[1]; i<=_OUT_DP; i++)); do
- _INP_NUM[1]=${_INP_NUM[1]}0
- done
- ###### Check for negative output, alter operator and set final negative output.
- if [[ "$_INP_OP" == "X" ]]; then
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "-" ]]; then
- _OUT_NEG=-
- fi
- elif [[ "$_INP_OP" == "+" ]]; then
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "--" ]]; then
- _OUT_NEG=-
- fi
- if [[ -n "${_INP_NEG[1]}" && -z "${_INP_NEG[0]}" ]]; then
- isgreater ${_INP_NUM[0]} ${_INP_NUM[1]}
- if [[ $? -eq 3 ]]; then
- _OUT_NEG=-
- _VAR_TMP=${_INP_NUM[0]}
- _INP_NUM[0]=${_INP_NUM[1]}
- _INP_NUM[1]=$_VAR_TMP
- fi
- _INP_OP=-
- fi
- if [[ -n "${_INP_NEG[0]}" && -z "${_INP_NEG[1]}" ]]; then
- isgreater ${_INP_NUM[0]} ${_INP_NUM[1]}
- if [[ $? -eq 3 ]]; then
- _VAR_TMP=${_INP_NUM[0]}
- _INP_NUM[0]=${_INP_NUM[1]}
- _INP_NUM[1]=$_VAR_TMP
- elif [[ %? -eq 1 ]]; then
- _OUT_NEG=-
- fi
- _INP_OP=-
- fi
- elif [[ "$_INP_OP" == "-" ]]; then
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "" ]]; then
- isgreater ${_INP_NUM[0]} ${_INP_NUM[1]}
- if [[ "$?" == "3" ]]; then
- _OUT_NEG=-
- _VAR_TMP=${_INP_NUM[0]}
- _INP_NUM[0]=${_INP_NUM[1]}
- _INP_NUM[1]=$_VAR_TMP
- fi
- fi
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "--" ]]; then
- isgreater ${_INP_NUM[0]} ${_INP_NUM[1]}
- if [[ "$?" == "3" ]]; then
- _VAR_TMP=${_INP_NUM[0]}
- _INP_NUM[0]=${_INP_NUM[1]}
- _INP_NUM[1]=_$_VAR_TMP
- elif [[ %? -eq 1 ]]; then
- _OUT_NEG=-
- fi
- fi
- if [[ -n "${_INP_NEG[1]}" && -z "${_INP_NEG[0]}" ]]; then
- _INP_OP=+
- fi
- if [[ -n "${_INP_NEG[0]}" && -z "${_INP_NEG[1]}" ]]; then
- _INP_OP=+
- _OUT_NEG=-
- fi
- elif [[ "$_INP_OP" == "/" ]]; then
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "--" ]]; then
- _OUT_NEG=""
- fi
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "-" ]]; then
- _OUT_NEG=-
- fi
- elif [[ "$_INP_OP" == "M" ]]; then
- if [[ -n "${_INP_NEG[0]}" ]]; then
- _OUT_NEG=-
- fi
- elif [[ "$_INP_OP" == "Y" ]]; then
- if [[ $_INP_EXP_PAR -eq 0 ]]; then
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "--" ]]; then
- _OUT_NEG=-
- _INP_EXP_DIV=1
- elif [[ -n "${_INP_NEG[1]}" ]]; then
- _INP_EXP_DIV=1
- elif [[ -n "${_INP_NEG[0]}" ]]; then
- _OUT_NEG=-
- fi
- else
- if [[ "${_INP_NEG[0]}${_INP_NEG[1]}" == "--" ]]; then
- if [[ "${_INP_NUM[1]:(-1)}" =~ [13579] ]]; then
- _OUT_NEG=-
- fi
- _INP_EXP_DIV=1
- elif [[ -n "${_INP_NEG[1]}" ]]; then
- _OUT_NEG=-
- _INP_EXP_DIV=1
- elif [[ -n "${_INP_NEG[0]}" ]]; then
- if [[ "${_INP_NUM[1]:(-1)}" =~ [13579] ]]; then
- _OUT_NEG=-
- fi
- fi
- fi
- fi
- ################################################################################
- ####################### Call the appropiate subroutines ########################
- ################################################################################
- #Numbers are now validated and normalised, _OUT_NEG shows if the result is negative
- #_OUT_DP holds the number of decimal places
- ######Pass the numbers
- if [[ "$_INP_OP" == "+" ]]; then
- _FIN_NUM=$(str_add ${_INP_NUM[0]} ${_INP_NUM[1]})
- elif [[ "$_INP_OP" == "-" ]]; then
- _FIN_NUM=$(str_sub ${_INP_NUM[0]} ${_INP_NUM[1]})
- elif [[ "$_INP_OP" == "X" ]]; then
- _OUT_DP=$(( (_OUT_DP * 2) + 1 ))
- _FIN_NUM=$(str_mult ${_INP_NUM[0]} ${_INP_NUM[1]})
- elif [[ "$_INP_OP" == "/" ]]; then
- _FIN_NUM=$(str_div ${_INP_NUM[0]} ${_INP_NUM[1]} $_DIV_DP)
- _OUT_DP=$((_DIV_DP - 1))
- elif [[ "$_INP_OP" == "M" ]]; then
- _FIN_NUM=$(str_div ${_INP_NUM[0]} ${_INP_NUM[1]} $_DIV_DP MOD)
- elif [[ "$_INP_OP" == "Y" ]]; then
- _FIN_NUM=$(str_exp ${_INP_NUM[0]} ${_INP_NUM[1]})
- if [[ $_INP_EXP_DIV -eq 1 ]]; then
- _FIN_NUM=$(str_div "1" $_FIN_NUM $_DIV_DP)
- _OUT_DP=$((_DIV_DP - 1))
- fi
- fi
- ######Reintroduce the decimal point, strip leading zeros and trailing zeros for decimal numbers and echo the result
- if [[ -z "$_OUT_DP" ]]; then
- _OUT_DP=0
- else
- _OUT_DP=$((_OUT_DP + 1))
- fi
- if [[ "$_OUT_DP" != "0" ]]; then
- for ((i=1; i<=_OUT_DP; i++)); do
- if [[ -z "${_FIN_NUM:$_OUT_DP}" ]]; then
- _FIN_NUM=0$_FIN_NUM
- fi
- done
- _FIN_NUM=${_FIN_NUM:0:-$_OUT_DP}.${_FIN_NUM:(-$_OUT_DP)}
- for ((i=_OUT_DP; i>=0; i--)); do
- if [[ "${_FIN_NUM:(-1)}" == "0" || "${_FIN_NUM:(-1)}" == "." ]]; then
- _FIN_NUM=${_FIN_NUM:0:-1}
- fi
- done
- fi
- while [ "${_FIN_NUM:0:1}" == "0" ]; do
- _FIN_NUM=${_FIN_NUM:1}
- done
- if [[ -z "${_FIN_NUM}" ]]; then
- _FIN_NUM=0
- fi
- if [ "${_FIN_NUM:0:1}" == "." ]; then
- _FIN_NUM=0$_FIN_NUM
- fi
- echo $_OUT_NEG$_FIN_NUM
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement