简体   繁体   中英

round integer if output comes in decimal

Bash shell scripting

var1=126
var2=16 
var1/var2 = 7.87

My requirement is if the output value comes in decimal, then add 1 to the integer ie in this case 7+1 =8 . How can I do that?

You can use the modulo operator % :

#! /bin/bash
var1=126
var2=16
(( result = var1 / var2 ))
(( var1 % var2 && ++ result )) # If there's a remainder, add 1 to result.
echo $result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM