We help IT Professionals succeed at work.

# URGENT!! Need Help Truncating a Number in a C-Shell Script - MAX points awarded

on
572 Views
Hello. I have been using this forum for a couple of years and finally came upon something I could not answer from the archives.

I am working on a C-Shell script and trying to take a number (i.e. 5.6784) and truncate it to only one decimal place. I have tried using the bc function with a scale, but it rounds the number. See below:

set xStripR = `echo "scale=1; \$xCoord1"

This will return what I am looking for except that 5.49 will return 5.4 and 5.51 will return 5.6.

Is there a way to truncate in C-Shell? Please help! This project has to be complete VERY soon.

Best regards,

SaucyJim
Comment
Watch Question

## View Solution Only

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
CERTIFIED EXPERT
Top Expert 2006

Commented:
set xStripR = `echo "scale=2; \$xCoord1 / 1" | bc`
CERTIFIED EXPERT
Top Expert 2006

Commented:
Please disregard my previous post.
CEO/Programmer
CERTIFIED EXPERT

Commented:
If you have a printf installed you can write:
printf "%.1f\n" 3.4234
3.4

That's quite handy isn't it ;-)

Regards
Friedrich

Commented:
Try This

echo \$xCoord1|awk -F\. '{print \$1"."substr(\$2,1,1)}'

Vinit
CEO/Programmer
CERTIFIED EXPERT

Commented:
Well if you have awk you have a printf. So no nead for substr tricks just write it
echo 1.2346 | awk '{printf ("%.1f\n", \$1)}'
1.2

Regards
Friedrich

Commented:

Sorry Friedrich, but the printf ("%.1f\n", \$1) rounds the number:

\$ printf "%.1f\n" 1.4632
1.5

Commented:
Try:

echo 5.51 | awk '{printf ("%.1f\n", int(\$1*10)/10)}'
5.5

Commented:

Ooops, but awk doesn't round:

\$ echo 5.51|awk '{printf "%.1f\n", \$1;}'
5.5

Commented:
> Ooops, but awk doesn't round

It doesn't?  Should be the same as any other printf (all should call the same C library function).

\$ echo 1.4632 | awk '{printf "%.1f\n", \$1;}'
1.5

Commented:
I dont think that
awk -F\. '{print \$1"."substr(\$2,1,1)}'
will round the number in any case.
Vinit

Commented:
echo 5.6784|awk '{print substr(\$0,1,index(\$0,".")+1)}'
# somhow similar to vinit_kain's suggestion

Commented:
Or use a math trick such as:

echo 1.4632 | awk '{printf "%.1f\n", \$1-0.05;}'

1.4

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

###### Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.