- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to calculate decimal number ?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 12:57 AM
01-10-2001 12:57 AM
How can I write a script to generate the result 0.5 ?
Thanks !
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 01:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 01:20 AM
01-10-2001 01:20 AM
Re: How to calculate decimal number ?
int main(int argc, char ** argv)
{
cout << atof(argv[1]) * atof(argv[2]) << endl;
}
Then you can use this program in your script to calculate decimal numbers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 01:23 AM
01-10-2001 01:23 AM
Re: How to calculate decimal number ?
'expr' won't give you anything but integers.
If you want nearly unlimited precision, you should use 'bc' like this:
echo "scale=2\n5/2" | bc
Result will be 0.50 as expected.
Adjust the scale according to your needs.
Best regards,
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 01:30 AM
01-10-2001 01:30 AM
Re: How to calculate decimal number ?
If you have many operations to do in your script, you could use a two-way pipe to 'bc' instead of running bc many times.
#!/usr/bin/sh
# This starts bc with a two-way pipe
bc |&
# print -p writes to the pipe
print -p scale=2
print -p 3/4
# read -p reads from the pipe
read -p myvar
echo $myvar
The script here above will echo '.75'
see manual for sh-posix
Best regards,
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 08:17 AM
01-10-2001 08:17 AM
Re: How to calculate decimal number ?
#!/usr/dt/bin/dtksh
float x
x=1.0/2 #one has to be floating point or it will perform integer arthimetic
print $x