Thread: Convert floating point scientific precision

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    5

    Post Convert floating point scientific precision

    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
    ==========================

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    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.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What did you try? Post your code, and I'll correct it.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    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.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    ohhh...So you telling that there is no direct way to print that right ... ok andurol462 thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert hexadecimal into floating point?
    By zenniz in forum C Programming
    Replies: 4
    Last Post: 03-11-2013, 09:59 AM
  2. Replies: 6
    Last Post: 07-15-2009, 10:50 AM
  3. Replies: 1
    Last Post: 04-03-2008, 01:17 AM
  4. how to convert decimal to floating point number
    By -EquinoX- in forum C Programming
    Replies: 98
    Last Post: 03-04-2008, 01:25 PM
  5. Precision based floating-point
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2006, 10:35 AM

Tags for this Thread