Home > Back-end >  I ask, why all my sprintf conversion when data type double to 0?
I ask, why all my sprintf conversion when data type double to 0?

Time:05-22

The code is as follows:
Char tmpbuffer [20].
Double U13Coef [8]={0.000000428344451, 0.000041556454936, 0.001508777414466, 0.02414235618335, 0.158289852824976, 1.54926741892562, 0.162671275351189, 0.55};
for (i=0; i<8; I++)
{
Sprintf (tmpbuffer, "% f", U13Coef [I]);
}
The output is always 0.000000; Don't understand, float type can output;

CodePudding user response:

 # include & lt; stdio.h> 
Int main () {
Char tmpbuffer [20].
Double U13Coef [8]={0.000000428344451, 0.000041556454936, 0.001508777414466, 0.02414235618335, 0.158289852824976, 1.54926741892562, 0.162671275351189, 0.55};
for (int i=0; i<8; I++) {
Sprintf (tmpbuffer, "%. 15 lg," U13Coef [I]);
Printf (" I: % d tmpbuffer:] [% s \ n ", I, tmpbuffer);
}
return 0;
}
//I: 0 tmpbuffer: [4.28344451 e-07]
//I: 1 tmpbuffer: [4.1556454936 e-05]
//I: 2 tmpbuffer: [0.001508777414466]
//I: 3 tmpbuffer: [0.02414235618335]
//I: 4 tmpbuffer: [0.158289852824976]
//I: 5 tmpbuffer: [1.54926741892562]
//I: 6 tmpbuffer: [0.162671275351189]
//I: 7 tmpbuffer: [0.55]
//

CodePudding user response:

 # include & lt; stdio.h> 
Int main () {
Char tmpbuffer [20].
Double U13Coef [8]={0.000000428344451, 0.000041556454936, 0.001508777414466, 0.02414235618335, 0.158289852824976, 1.54926741892562, 0.162671275351189, 0.55};
for (int i=0; i<8; I++) {
Sprintf (tmpbuffer, "%. 15 lf U13Coef [I]);
Printf (" I: % d tmpbuffer:] [% s \ n ", I, tmpbuffer);
}
return 0;
}
//I: 0 tmpbuffer: [0.000000428344451]
//I: 1 tmpbuffer: [0.000041556454936]
//I: 2 tmpbuffer: [0.001508777414466]
//I: 3 tmpbuffer: [0.024142356183350]
//I: 4 tmpbuffer: [0.158289852824976]
//I: 5 tmpbuffer: [1.549267418925620]
//I: 6 tmpbuffer: [0.162671275351189]
//I: 7 tmpbuffer: [0.550000000000000]
//

CodePudding user response:

Such a change, for your reference:
 # include 
Int main ()
{
Int I;
Char tmpbuffer [8] [20].
Double U13Coef [8]={0.000000428344451, 0.000041556454936, 0.001508777414466, 0.02414235618335,
0.158289852824976, 1.54926741892562, 0.162671275351189, 0.55};
for (i=0; i<8; I++)
{
Sprintf (tmpbuffer [I], "% 18.15 f," U13Coef [I]);
Printf (" % s \ n ", tmpbuffer [I]);
}

return 0;
}


//0.000000428344451
//0.000041556454936
//0.001508777414466
//0.024142356183350
//0.158289852824976
//1.549267418925620
//0.162671275351189
//0.550000000000000
//please press any key to continue...

CodePudding user response:

 # include & lt; stdio.h> 

Int main (void)
{
int i;
//char tmpbuffer [20].
Char tmpbuffer [64].
Double U13Coef [8]={0.000000428344451, 0.000041556454936, 0.001508777414466, 0.02414235618335, 0.158289852824976, 1.54926741892562, 0.162671275351189, 0.55};

for (i=0; i<8; I++) {
Sprintf (tmpbuffer, "%. 15 lf U13Coef [I]);
Puts (tmpbuffer);
}
return 0;
}

For your reference ~
Note the preserve of decimal places.

Default is six, the six extra does not display;
Nature does not convert into the buffer,

CodePudding user response:

reference QZJHJXJ reply: 3/f
this change, for reference:
 # include 
Int main ()
{
Int I;
Char tmpbuffer [8] [20].
Double U13Coef [8]={0.000000428344451, 0.000041556454936, 0.001508777414466, 0.02414235618335,
0.158289852824976, 1.54926741892562, 0.162671275351189, 0.55};
for (i=0; i<8; I++)
{
Sprintf (tmpbuffer [I], "% 18.15 f," U13Coef [I]);
Printf (" % s \ n ", tmpbuffer [I]);
}

return 0;
}


//0.000000428344451
//0.000041556454936
//0.001508777414466
//0.024142356183350
//0.158289852824976
//1.549267418925620
//0.162671275351189
//0.550000000000000
//please press any key to continue...

Pay attention to the length of column, 18 + 15 & gt; 20, so the length of 20 is inappropriate,
  • Related