-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrounding.sh
More file actions
executable file
·31 lines (24 loc) · 799 Bytes
/
rounding.sh
File metadata and controls
executable file
·31 lines (24 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash
# I wrote this doing hackerrank exceriszes where rounding precision on math
# equations, and decimal precision between bc and whatever they used left
# something to be desired. This rounds up so you can pass the test. Does a math
# equation with bc, but rounds up.
# Fill this out
equation="" #Put your math equation here.
scale=4 # should be one greater than you need. Gets the next digit to compute
# Rounding
# /options
number=$(echo "scale=${scale};${equation}"| bc -l)
i=$((${#number}-1))
last_digit=${number:$i:1}
i=$(( $i - 1))
second_last_digit=${number:$i:1}
number=${number:0:$i}
ext=""
if [ $last_digit -ge 5 ];then
ext=$((${second_last_digit} + 1))
elif [ $last_digit -lt 5 ];then
ext=${second_last_digit}
fi
number="${number}${ext}"
echo $number