user3742796
user3742796

Reputation: 65

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 i.e in this case 7+1 =8. How can I do that?

Upvotes: 0

Views: 70

Answers (1)

choroba
choroba

Reputation: 241848

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

Upvotes: 1

Related Questions