Thread: fixing numbers

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    fixing numbers

    please, how do i fix a number in c++,
    my problem is that a float number returns me a lot of decimals that i do not need to display so I want to cut those off the number

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    use setprecision... I forgot the code and dont have any access to any of the programs i've written with it in there tho... but you can look it up...

    //edit: setprecision doesn't round the number, it truncates... i.e.:
    with setprecision set to two ( cout<<setprecision(2); ):
    6.1564 = 6.15
    6.150041 = 6.15
    483.480001 = 183.48
    483.4863487 = 483.48
    Last edited by major_small; 10-15-2003 at 06:09 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by major_small
    use setprecision... I forgot the code and dont have any access to any of the programs i've written with it in there tho... but you can look it up...
    major_small, you mean to tell me that you don't remember the majic formula that every programmer should recite without hesitation when waken up in the middle of the night??? just kidding, hehe

    Code:
      cout.setf(ios::showpoint);
      cout.setf(ios::fixed);
      cout.precision(/*number of digitts here*/);
    axon

    EDIT:: BTW this is within iomanip lib

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^haha... i haven't used it in so long... i was actually the only one to memorize it in my high school programming class...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    i thought Set precision does round numbers off... it does when i do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM