We help IT Professionals succeed at work.

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

SaucyJim
SaucyJim asked
on
572 Views
Last Modified: 2013-12-26
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

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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.
fridomCEO/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
Try This

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

Vinit
fridomCEO/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

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

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

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


Ooops, but awk doesn't round:

$ echo 5.51|awk '{printf "%.1f\n", $1;}'
5.5
> 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
I dont think that
awk -F\. '{print $1"."substr($2,1,1)}'
will round the number in any case.
Vinit


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.

Get Access
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
Technical Department Head

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

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
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.
Join our community and discover your potential

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.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.