Thread: How to take only two decimals of float without rounding?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  2. #2
    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, 02: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, 05:26 PM

Tags for this Thread