How to take only two decimals of float without rounding?

This is a discussion on How to take only two decimals of float without rounding? within the C Programming forums, part of the General Programming Boards category; Hi I am brand new to C and am trying to understand how the float and double variables work. What ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Question How to take only two decimals of float without rounding?

    Hi I am brand new to C and am trying to understand how the float and double variables work. What I am trying to do is initialize a variable to say, x = 6.789. And then from there just print out a basic statement such as:

    printf("The furniture cost $ %.2f", x);

    However whenever I do this it always rounds the variable to $6.79. Is there a way to make it say $6.78? I just need help understanding how it works, if anyone could just get me started that would be great.

    Thanks Cody

    IGNORE THREAD: It was a very basic understanding assignment I was working on but the teacher has announced a there was a typo which solves my problem, I would delete the thread but im not sure how, sorry.
    Last edited by Cromphf; 01-23-2011 at 10:45 PM. Reason: Typo

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Someone correct me if I'm wrong, but this line
    Code:
    %.2f
    Will "create" a float with 2 decimals from the float you have (".2f" seems quite obvious). And the correct mathematical way to do this is to round it.

    Being a cnewbie I don't know if there exists a easy way to specify x decimals without rounding. My first impulse would be to treat the float variable as a string and just cut away everything after .xx (all decimals after the second) manually.
    Last edited by cnewbie1; 01-24-2011 at 10:41 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    Look up the floor() function in IIRC math.h; might not do what you want but it or round() with division/multiplication should.

    Homemade code without math.h, not tested

    printf("The furniture cost $ %.2f", (int)(x*100)/100.0);

    Homemade code with math.h, not tested
    printf("The furniture cost $ %.2f", floor(x*100)/100.0);

    Tim S.
    Last edited by stahta01; 01-24-2011 at 10:46 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Quote Originally Posted by cnewbie1 View Post
    And the correct mathematical way to do this is to round it.

    Being a cnewbie I don't know if there exists a easy way to specify x decimals without rounding. My first impulse would be to treat the float variable as a string and just cut away everything after .xx (all decimals after the second) manually.
    Rounding is certainly the behavior that most people would expect, but I wouldn't say it's the correct way. Truncation is perfectly valid and correct, even if it's less used than rounding. The IEEE 754 floating point standard, which is what most systems conform to, specifies 5 rounding methods, including the truncation that the OP was interested in. Whether the OP has a fully conforming implementation and how s/he will set the rounding method, I can't say.

    Quote Originally Posted by stahta01 View Post
    Look up the floor() function in IIRC math.h; might not do what you want but it or round() with division/multiplication should.
    Try trunc() with some multiplication/division. It is also in math.h and avoids the problems you will encounter with floor and negative arguments.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    anduril: Yup, you're right of course. Bad choice of words on my part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21