Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Math (rounding to the nearest hundreth)

pnezzypnezzy Posts: 2Member
Dunno, maybe this is a stupid question, but I've had trouble finding anything about rounding numbers in basic C++. I'm sorta new at this so if yall could give me a bit of help on this. Anyways, later!

Comments

  • Josh CodeJosh Code Posts: 675Member
    : Dunno, maybe this is a stupid question, but I've had trouble finding anything about rounding numbers in basic C++. I'm sorta new at this so if yall could give me a bit of help on this. Anyways, later!
    :
    :


    Here is some code for a console app that should help:
    [code]
    [green]#include [/green]
    [green]#include [/green]

    [b]int[/b] main([b]int[/b] argc, [b]char[/b] *argv[])
    {
    [b]float[/b] f;
    [b]float[/b] rounded;
    f = 2.71828182845904523536028747135266;
    rounded = (int)(f*100); [italic]// rounded = 271[/italic]
    cout << rounded;
    cout << '
    '; [italic]// break the line[/italic]
    rounded /= 100; [italic]// rounded = 2.71[/italic]
    cout << rounded;
    [b]while[/b] (0==0) [italic]// keep the console open so you can see the outputs[/italic]
    {}
    [b]return[/b] 0;
    }[/code]

    The "(int)" means the result of the multiplication is truncated. It is a little different from rounding. With normal rounding, it would round up to 2.72 but instead the rest of the number is ignored.


  • rollerolle Posts: 114Member
    : : Dunno, maybe this is a stupid question, but I've had trouble finding anything about rounding numbers in basic C++. I'm sorta new at this so if yall could give me a bit of help on this. Anyways, later!
    : :
    : :
    :
    :
    : Here is some code for a console app that should help:
    : [code]
    : [green]#include [/green]
    : [green]#include [/green]
    :
    : [b]int[/b] main([b]int[/b] argc, [b]char[/b] *argv[])
    : {
    : [b]float[/b] f;
    : [b]float[/b] rounded;
    : f = 2.71828182845904523536028747135266;
    : rounded = (int)([red][b][size=4]([/size][/b][/red]f[red][b][size=4]+0.005)[/b][/red][/size]*100); [italic]// rounded = 271[/italic]
    : cout << rounded;
    : cout << '
    '; [italic]// break the line[/italic]
    : rounded /= 100; [italic]// rounded = 2.71[/italic]
    : cout << rounded;
    : [b]while[/b] (0==0) [italic]// keep the console open so you can see the outputs[/italic]
    : {}
    : [b]return[/b] 0;
    : }[/code]
    :
    : The "(int)" means the result of the multiplication is truncated. It is a little different from rounding. With normal rounding, it would round up to 2.72 but instead the rest of the number is ignored.
    :
    :
    :
    [blue]If you would rather have the value rounded and not truncated, see red[/blue]
  • pnezzypnezzy Posts: 2Member
    Thanks a lot! These are a lot simpler than the way I figured out. Awesome.
Sign In or Register to comment.