LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-18-2009, 01:59 AM   #1
raman.kumar
LQ Newbie
 
Registered: May 2009
Posts: 1

Rep: Reputation: 0
Angry How to do arithmatic operation on floating number by using expr ???


As i know that expr can not be used by decimal number....can anyone tell me that can i use expr and do arithmatic operation on decimal number ????
 
Old 05-18-2009, 02:35 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can use either bc or awk. Using bc you have to specify the scale that is the total number of decimal digits after the decimal point. For example:
Code:
$ echo "scale=4; 3.4 / 2.0" | bc
1.7000
$ echo "scale=9; 3.4 / 2.0" | bc
1.700000000
In awk you can perform floating point arithmetic and print the result accordingly using printf:
Code:
$ awk 'BEGIN{printf "%11.9f\n",3.4/2.0}'
1.700000000
 
Old 05-18-2009, 07:02 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by raman.kumar View Post
As i know that expr can not be used by decimal number....can anyone tell me that can i use expr and do arithmatic operation on decimal number ????
Semantics:
"decimal number" means base 10. You mean floating point (not "floating")---or maybe "non-integer".

As you stated, "expr" does not do floating point. In fact, BASH does not do floating point. You need something like the suggestions from colucix.

Last edited by pixellany; 05-18-2009 at 07:03 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) lumix Linux - General 5 02-25-2009 05:31 PM
Different floating point operation results on AMD and Intel ykwok Linux - General 4 06-17-2008 06:56 AM
floating point number vijraj Programming 2 03-07-2007 04:17 PM
using expr to prinbt floating point result. max_rsr Linux - General 1 03-14-2005 01:43 AM
how to use expr to print floating point values max_rsr Linux - Newbie 1 03-12-2005 10:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration