Convert floating point variable to integer? Convert floating point variable to integer? bash bash

Convert floating point variable to integer?


Assuming $myduration is a decimal or integer

$ myduration=6.5$ myduration=$( printf "%.0f" $myduration )$ echo $myduration6


You can do this:

float=1.23int=${float%.*}

I am using this on bash.


It's not entirely clear, but I think you're asking how to convert a floating-point value (myduration) to an integer in bash. Something like this may help you, depending on which way you want to round your number.

#!/bin/bashfloor_val=ceil_val=function floor() {    float_in=$1    floor_val=${float_in/.*}}function ceiling() {    float_in=$1    ceil_val=${float_in/.*}    ceil_val=$((ceil_val+1))}float_val=$1echo Passed in: $float_valfloor $float_valceiling $float_valecho Result of floor: $floor_valecho Result of ceiling: $ceil_val

Example usage:

$ ./int.sh 12.345Passed in: 12.345Result of floor: 12Result of ceiling: 13