setprecision??

This is a discussion on setprecision?? within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm trying to use set precision but it only makes to answer of an equation come out wit no ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    setprecision??

    Hey,
    I'm trying to use set precision but it only makes to answer of an equation come out wit no decimal places but I want a number to go into the equation with no decimal places.
    Is there any way that you can put setprecision into an equation?
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    Provide the smallest and simplest program (or code snippet) that demonstrates your problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    h = round ((d/x * 48) /5);
    n = round ((e/x * 48) /5);
    cout << fixed << setprecision(0);
    r = (h*5);
    y = (n*5);

    I need this to come out as multiple of 5 but it wont round the h and n when they are being multiplied by 5. It only rounds them if it stops before the multiply by 5 bit.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by rachael033
    Hey,
    I'm trying to use set precision but it only makes to answer of an equation come out wit no decimal places but I want a number to go into the equation with no decimal places.
    Is there any way that you can put setprecision into an equation?
    Thanks
    setprecision does absolutely nothing to the data, and there for does nothing to equations that handle the data. All it does is format the output of floats. If you want the data to be rounded without decimals, then cast the float as an int.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    But then if I do that when I compile it it has a warning that comes up saying warning - converting 'float' to 'int'. I want to try to avoid that.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    No, you wouldn't get a warning if you were typecasting it. I'd guess what you're doing is actually just assigning it to an int
    Code:
    float foo = 5.6;
    int bar = foo;  // This produces a warning for the implicit cast
    int baz = (int)foo;  // This doesn't produce a warning
    
    
    // Now keeping in mind that this doesn't round, it just knocks off the decimal, then
    // consider trying something like this:
    
    int qux;
    if (foo - (int)foo < .50)
        qux = (int)foo;
    else
        qux = (int)foo + 1;
    
    // Lastly, if you're not interested in creating a new int variable. Then you can 
    // use the ?: conditional operator in any statement, assuming you don't mind the clutter:
    
    std::cout << (foo - (int)foo < .50 ? (int)foo : (int)foo + 1);
    Last edited by SlyMaelstrom; 03-22-2006 at 01:39 AM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setprecision()
    By hallo007 in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2006, 08:38 AM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 01:26 PM
  3. setprecision()
    By OnionKnight in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2005, 08:08 PM
  4. Setw() and setprecision?
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2003, 08:20 AM
  5. setprecision() Help
    By frgmstr in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 02:24 PM

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