This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Solved

Posted on 1997-03-03

Take a look at the following code:

#include <conio.h>

#include <iostream.h>

void main()

{

float MyVariable, YourVariable, OurVariable;

MyVariable = 3.456;

YourVariable = 3.75;

OurVariable = MyVariable + YourVariable;

cout << "Here is the value of OurVariable :" << OurVariable << endl;

}

I was wondering how I can round numbers in two ways:

1) How can I round both values before I total them?

2) How can I round the value of there sum to a certain number of decimal places?

#include <conio.h>

#include <iostream.h>

void main()

{

float MyVariable, YourVariable, OurVariable;

MyVariable = 3.456;

YourVariable = 3.75;

OurVariable = MyVariable + YourVariable;

cout << "Here is the value of OurVariable :" << OurVariable << endl;

}

I was wondering how I can round numbers in two ways:

1) How can I round both values before I total them?

2) How can I round the value of there sum to a certain number of decimal places?

1 Comment

int i;

float f = 3.456;

i = (int)f;

this will round a float:

int i;

float f = 3.456;

i = (int)(f + 0.5);

changing:

OurVariable = MyVariable + YourVariable;

to:

OurVariable = (int)(MyVariable+0.5) + (int)(YourVariable+0.5);

should do it.

Rounding to a specific number of digits:

try

float f = ((int)(3.45678 * 100))/100);

the (int)(3.45678 * 100) evaluates to 345 and dividing by 100 gives 3.45.

For n decimal places multiply and divide by 10^n. of Course the limits of precision for floats and doubles still have to be taken into consideration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join the community of 500,000 technology professionals and ask your questions.