Math (rounding to the nearest hundreth) - Programmers Heaven

Howdy, Stranger!

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

Categories

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.