How to convert decimal numbers to C?

This is my print statement:

printf("%d %f\n",kPower, raisePower); 

This is my conclusion:

 -4 0.000100 -3 0.001000 -2 0.010000 -1 0.100000 0 1.000000 1 10.000000 2 100.000000 3 1000.000000 4 10000.000000 

I want it to be printed as follows:

enter image description here

UPDATE

So, I made my positive values:

 -4 0.0 -3 0.0 -2 0.0 -1 0.1 0 1.0 1 10.0 2 100.0 3 1000.0 4 10000.0 

This is my new code:

 printf("%d %10.1f\n",kPower, raisePower); 

I do not know if I should make a for loop to print each of them (positive results versus negative result) in a different format?

+6
source share
7 answers

Try computing the powers first using pow() from math.h , and then:

You can use% 10f to precede the number of spaces in the example, just 10 spaces:

 printf ("Preceding with blanks: %10f \n", 10000.01); 

Source: cplusplus.com

+2
source
 #include <stdio.h> char *get_number_formatted(double f) { static char buf[128]; // this function is not thread-safe int i, j; i = snprintf(buf, 128, "%20.10f", f) - 2; for (j = i - 8; i > j; --i) if (buf[i] != '0') break; buf[i + 1] = '\0'; return buf; } int main(void) { int i; for (i = -4; i < 5; ++i) printf("%5d %s\n", i, get_number_formatted(pow(10.0, i))); return 0; } 

http://ideone.com/KBiSu0

Output:

  -4 0.0001 -3 0.001 -2 0.01 -1 0.1 0 1.0 1 10.0 2 100.0 3 1000.0 4 10000.0 

printf() cannot print the variable length of decimal digits, so basically I printed the formatted number to the buffer and then cut the excess of zeros.

+3
source

Basically you can use variable length to accomplish this:

 printf("%d %.*lf", kPower, -kPower, raisePower); 

The advantage over other methods is that this method does not need additional buffer (s)

+2
source

With a little modf help modf you can use %g to skip trailing zeros and \b to skip trailing zeros:

 #include <stdio.h> #include <math.h> int main(void) { int i, iarr[] = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; double darr[] = {0.0001, 0.001, 0.01, 0.1, 1., 10., 100., 1000., 10000.}; double intpart, fractpart; for (i = 0; i < 9; i++) { fractpart = modf(darr[i], &intpart); if (fractpart == 0.0) printf("%10d%10d.0\n", iarr[i], (int)intpart); else printf("%10d%10d\b%g\n", iarr[i], (int)intpart, fractpart); } return 0; } 

Output:

  -4 0.0001 -3 0.001 -2 0.01 -1 0.1 0 1.0 1 10.0 2 100.0 3 1000.0 4 10000.0 
+2
source

Try this sample code

 float y[7]={0.000100f,0.0010f,0.0100,0.1000f,1.0f,10.000f,100.00f}; int a[7]={-4,-3,-2,-1,0,1,2}; for(int i=0;i<7;i++) printf("%2d%20f\n",a[i],y[i]); 

The result will be like this.

enter image description here

+1
source

You can use sprintf and then trim the zeros. This is the same idea as @Havenard, but writing spaces above zeros instead of cutting the line. And my C-style is slightly different from FWIW. My style is that I do not want to count or do arithmetic in my head; what for optimizer C for :).

 #include <math.h> #include <stdio.h> #include <string.h> int main() { int kPower; for(kPower=-4; kPower<5; kPower++){ enum { bufsize = 2+5+10+1+4+1+1 }; char buf[bufsize]; int j,n,i; double raisePower = pow(10,kPower); //printf("%2d %10.4f\n",kPower,raisePower); snprintf(buf,bufsize,"%2d %10.4f\n",kPower,raisePower); j=strchr(buf,'.')-buf; j+=1; n=strchr(buf+j,'\n')-buf; for (i=n-1; i>j; i--) if (buf[i]=='0') buf[i]=' '; else break; printf("%s",buf); } return 0; } 

Output:

 -4 0.0001 -3 0.001 -2 0.01 -1 0.1 0 1.0 1 10.0 2 100.0 3 1000.0 4 10000.0 
+1
source

use printf as follows:

 printf( "%5d %10.1f\n",kPower, raisePower); this will result in kPower being printed, right justified, in 5 columns - sign in the right place this will result in raisePower being printed with: 10 columns leading 0s replaced by spaces except 1 digit (could be 0) to the left of the decimal point 1 (rounded) digit to the right of the decimal point - signs being printed at the proper location decimal point being aligned 
0
source

Source: https://habr.com/ru/post/986106/


All Articles