• Status: Solved
• Priority: Medium
• Security: Public
• Views: 304

# Unix shell script, How to do rounding of number ?

Below is my code. I want to check total number of lines and divided it by 2.

Suppose total no of lines =24 then I will divide by 2 = 12 .

But  when total no of lines =23 and then I will divide by 2 it gives 11 . I want 12 because actulay value is 11.5

ncnt=\$((`wc -l < AMS_Total_Business_ID.txt` / 2))

0
digs developer
• 5
• 3
1 Solution

Commented:
ncnt=\$(( ( `wc -l < AMS_Total_Business_ID.txt` + 1 ) / 2))
0

Author Commented:
But when I divide by 1000 is is not shown correct nmber like by 2.

ncnt=\$(( ( `wc -l < AMS_Total_Business_ID.txt` + 1 ) / 1000))

division by value should be change on run time. It may be 500 ,900 or 1000

0

Commented:
ncnt=\$(( ( `wc -l < AMS_Total_Business_ID.txt` + 500 ) / 1000))
0

Author Commented:
Thank you !
0

Author Commented:
I have one text file ABC_T_B_ID.txt which contains values in one column.
The values may be contain 1 or 1000 or 45000 and so on.

So my requirement is count the total line in a file and divided it by 1000 whatever the value comes that value should be ceil.

suppose
1. no of lines = 1000
1000/1000 = 1

2. no of lines = 2000
2000/1000 = 2

3. no of lines = 10
10/1000 = 0.01   I need as 1

4. no of lines = 411
411/1000 = 0.411   I need as 1

4. no of lines = 9898
9898/1000 = 9.898   I need as 10

4. no of lines = 9001
9001/1000 = 9.001   I need as 10

Below is my code.

ncnt=\$(( ( `wc -l < ABC_T_B_ID.txt` + 500 ) / 1000))
0

Author Commented:
0

Author Commented:
Hello Experts,

0

Commented:
Seems you always want to round up:

ncnt=\$(( ( `wc -l < ABC_T_B_ID.txt` + 999 ) / 1000))

More generally:

DIV=1000
ncnt=\$(( ( `wc -l < ABC_T_B_ID.txt` + (\$DIV-1) ) / \$DIV))

Note Always rounding up will (for example) give 46 for 45001 lines divided by 1000. Is this desired?
0

Commented:
ncnt=\$(( ( `wc -l < ABC_T_B_ID.txt` + 999 ) / 1000))
0

## Featured Post

• 5
• 3
Tackle projects and never again get stuck behind a technical roadblock.