sprintf for %f

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suunknown
    New Member
    • Oct 2007
    • 3

    sprintf for %f

    Hi,

    I am new to C++. I need to format a float variable to 4d.9d ( Two numbers before decimal and 8 after decimal ).

    How can I use sprintf for this?
  • Hunderpanzer
    New Member
    • Jul 2007
    • 60

    #2
    Originally posted by suunknown
    Hi,

    I am new to C++. I need to format a float variable to 4d.9d ( Two numbers before decimal and 8 after decimal ).

    How can I use sprintf for this?

    Sorry I don't know much about that, but I found this for you:




    If this doesn't help, I'm sure someone will come and give you better advice.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Read page 243 in The ANSI C Programming Language by K&R.

      Afteer the % you can specify your field.

      A number is the field width. Use a negative number for left-justify.
      A period follows the field width by the precision. In the case of a float this will be the number of decimals to the right of the decimal point.

      So, to do 4d.9d on a float you would:

      %13.9f

      and it still won't work!

      You see, a float has only six significant digits. You need to use a double.

      Comment

      • suunknown
        New Member
        • Oct 2007
        • 3

        #4
        Hi,

        Thanks for the reply.

        I am using double only. I need total lenght of 14.

        The decimal places can be from 0-12.

        For ex: the number can be 12.123456789012 or it can be without decimal places like 1234567890123

        Total length cannot exceed 14.

        I have to convert that using sprintf to char's

        I am using:
        sprintf(buffer, '%14f', value );

        By using so, when I have decimal places greater that 6, it is rounding to 6 decimal places.

        What can I use in this case?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You must have misunderstood my post.

          [code=c]
          printf("%14.9f\ n",12.123456789 012);
          [/code]

          This says in a field of 14, right-justify this double and show 9 decimal places.

          Comment

          • suunknown
            New Member
            • Oct 2007
            • 3

            #6
            Thanks again for your reply.

            But I donnot know variable precision or scalar length upfront.

            It can be in the following way

            1) 1234.123456789 (total of 14 chars )

            or it can be

            2) 123456789.1234 ( total 14 chars )

            or it can be

            3) 123456.1234567 ( total 14 chars )

            It should be 14chars length (scalar & precision will vary). In this case how I should have it in sprintf.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by suunknown
              It should be 14chars length (scalar & precision will vary). In this case how I should have it in sprintf.
              Are you reading my posts??

              1234.123456789 is "%14.9f"
              123456789.1234 is "%14.4f"
              123456.1234567 is "%14.7f"

              Get a copy of of the ANSI C Programming Language by K&R.

              This has all been clearly documented.

              Comment

              Working...