Can we change the floating point number format from scientific format to below example format ?
FROM TO
==========================
2.06374E-03 ---> 206370-8
-4.30311E-01 ---> -.430310
-4.28146E-04 ---> -42815-8
==========================
Can we change the floating point number format from scientific format to below example format ?
FROM TO
==========================
2.06374E-03 ---> 206370-8
-4.30311E-01 ---> -.430310
-4.28146E-04 ---> -42815-8
==========================
Well, scanf has format specifiers (link) for scientific notation, so it can scan them in. Then you use printf with the right format specifiers for the output format you want.
Thank you anduril462 for your response. I tried lot of things but I did not get the required format output (ex. 2.06374E-03 ---> 206370-8) , so can you help me how to print the float value in th above mentioned format.
What did you try? Post your code, and I'll correct it.
Code:include<stdio.h> main() { float abc = 2.06374E-03; printf("Required format is : 206370-8\n\nBut I am getting the below formats only\n------------------------------------------\n"); printf("%f\n",abc); printf("%g\n",abc); printf("%e\n",abc); printf("%1.5f\n",abc); printf("------------------------------------------\n"); getchar(); }
anduril462, the output should be in the format listed down below.
1.The output should be 8 characters long which may also include a negative sign '-'(if applicable).
2.The output format (ex : 206370-8) should not include a scientific notation 'e' or 'E'.
Last edited by sachinvg; 03-31-2014 at 11:29 PM.
Ahh, I get what you want now. printf doesn't have the exact format you're after. You will have to concoct something yourself.
You can try manipulating the input string to separate out the mantissa and exponent and reading them into two variables:
Turn "2.06374E-03" into two strings "2.06374" and "-03"
Store the first in a variable mantissa, the second in a variable exponent
mantissa = 2.06374
exponent = -3
Figure out where the decimal should be to get the format you want, and multiply/divide mantissa by 10 while incrementing/decrementing exponent by 1.
Print them in the format you want with printf.
ohhh...So you telling that there is no direct way to print that right ... ok andurol462 thank you