#### Howdy, Stranger!

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

#### Categories

Welcome to the new platform of Programmers 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 it's 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)

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!

· ·

• 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.

· ·
• 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]
· ·
• Posts: 2Member
Thanks a lot! These are a lot simpler than the way I figured out. Awesome.
· ·