Home > Back-end >  O rounding function
O rounding function

Time:03-20

Function interface definition:
Double Round (double x, int n);
The referee sample testing program:
# include & lt; stdio.h>
Double Round (double x, int d);
Int main (void)
{
int n;
double x;
Lf the scanf (" % % d ", & amp; X, & amp; N);
Printf (" % g \ n ", Round (x, n));

return 0;
}
Please fill in the answer here */*/
Input the sample:
Given a set of input here, for example:

123.456 2
The output sample:
Here is given the corresponding output, for example:

123.46

CodePudding user response:

This to
I can't run the zha
Give me a this id returned below 1 exit status
Excuse me, does this do

CodePudding user response:

Turn floating-point integer, is to remove the decimal part (can also be called to zero for)
Such as:
(int) 1.6 & gt; 1
(int) 1.3 & gt; 1
When the float is a positive number and 0.5
(int) (1.6 + 0.5)=(int) 2.1 & gt; 2
(int) (1.3 + 0.5)=(int) 1.8 & gt; 1
Don't do theory prove directly said when floating-point positive , floating-point + 0.5 in into integer as rounded

When the float is negative when they need 0.5

Specific code
 
#include

Int Fun_To float (n)
{
Float a=0.5 f;
Int sign=* (int *) & amp; N & amp; 0 x80000000;//plus or minus sign
//0 x3f000000 is floating point 0.5 directly expressed in hexadecimal here
Int h=0 x3f000000 | sign;//according to the symbol to 0.5 or 0.5 +
Return the n + * (float *) & amp; h;//floating-point add +/- 0.5
}

Int main (int arg c, char * argv [])
{

Printf (" % d \ n ", Fun_To (3.6));

return 0;
}



CodePudding user response:


Level
The medal of Blank
Sorry I didn't look at the title

If you want,
Still can use the above methods
With floating point multiply by 10 ^ n,
After rounding
In divided by 10 ^ n
  • Related