LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2006, 11:19 AM   #1
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Rep: Reputation: 35
How to round a float up to 2 figures after a point in cpp?


I have smth like 8.93536, how to make it 8.93?

Thanks.
 
Old 03-11-2006, 11:52 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Note to properly round your example 8.93536,
it should become 8.94, not 8.93 because the next digit is >=5.
This example does this correctly.
Code:
//usr/bin/gcc -O2 -Wall -Werror -o t t.c -lm; exit 0

#include <stdio.h>

extern double trunc(double);

int main(int argc, char *argv[]) {
    double x;

    scanf("%lf",&x);

    //round to nearest 0.01
    x=trunc(x*100.0+0.5)/100.0;

    printf("%lf\n",x);
    return 0;
}

Last edited by randyding; 03-11-2006 at 11:54 AM.
 
Old 03-11-2006, 12:24 PM   #3
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
And where is implementation of double trunc(double)?
 
Old 03-11-2006, 04:55 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
In the code example, the first line shows (assuming source file named t.c)
Code:
gcc -O2 -Wall -Werror -o t t.c -lm
The -lm on the end means link with the math library.
If you want to see what funcitons are implemented in the math library type the following command.
Code:
$ nm /usr/lib/libm.so | grep W
 
Old 03-12-2006, 01:29 PM   #5
Crobat
Member
 
Registered: Dec 2005
Distribution: Slackware 10.2, Slackware 11.0
Posts: 135

Rep: Reputation: 15
Not sure if it's in a unix environment, but try including the iomanip library. There should be a method called setPrecision(). Try a google of iomanip.
 
  


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
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 05:52 PM
What the heck... Problems with stdarg? Cannot put out float figures. Zacharias Programming 3 08-07-2004 10:03 PM
debian dpkg cpp depends on cpp error darkleaf Linux - Software 2 06-25-2004 02:47 AM
PS2 mouse goes crazy [it goes round n round n round...] goci Linux - Hardware 2 10-09-2003 09:15 AM
Now I have gcc/cpp/cpp, but still can't install sfingerh Linux - Software 2 07-30-2002 04:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration