Print a double as a decimal with a specified precision












1












$begingroup$


How could I make this code more effective?



static void     print_decimal(double decimal, int precision)
{
size_t integer;

while (precision--)
{
decimal *= 100.;
integer = (int)decimal;
if (!precision && (integer % 10) >= 5 && decimal != (double)integer)
integer += 10;
decimal *= .10;
integer /= 10;
printf("%i", integer);
decimal -= integer;
}
}


Example of calling that show me the decimals of a double with precision



print_decimal(65.226, 4)










share|improve this question











$endgroup$

















    1












    $begingroup$


    How could I make this code more effective?



    static void     print_decimal(double decimal, int precision)
    {
    size_t integer;

    while (precision--)
    {
    decimal *= 100.;
    integer = (int)decimal;
    if (!precision && (integer % 10) >= 5 && decimal != (double)integer)
    integer += 10;
    decimal *= .10;
    integer /= 10;
    printf("%i", integer);
    decimal -= integer;
    }
    }


    Example of calling that show me the decimals of a double with precision



    print_decimal(65.226, 4)










    share|improve this question











    $endgroup$















      1












      1








      1





      $begingroup$


      How could I make this code more effective?



      static void     print_decimal(double decimal, int precision)
      {
      size_t integer;

      while (precision--)
      {
      decimal *= 100.;
      integer = (int)decimal;
      if (!precision && (integer % 10) >= 5 && decimal != (double)integer)
      integer += 10;
      decimal *= .10;
      integer /= 10;
      printf("%i", integer);
      decimal -= integer;
      }
      }


      Example of calling that show me the decimals of a double with precision



      print_decimal(65.226, 4)










      share|improve this question











      $endgroup$




      How could I make this code more effective?



      static void     print_decimal(double decimal, int precision)
      {
      size_t integer;

      while (precision--)
      {
      decimal *= 100.;
      integer = (int)decimal;
      if (!precision && (integer % 10) >= 5 && decimal != (double)integer)
      integer += 10;
      decimal *= .10;
      integer /= 10;
      printf("%i", integer);
      decimal -= integer;
      }
      }


      Example of calling that show me the decimals of a double with precision



      print_decimal(65.226, 4)







      c formatting floating-point






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      200_success

      129k15153415




      129k15153415










      asked 9 hours ago









      kenken

      376




      376






















          2 Answers
          2






          active

          oldest

          votes


















          0












          $begingroup$

          Well, there's the obvious stuff, like whitespace and declaring variables at their first point of use. For example:



          static void     print_decimal(double decimal, int precision)
          {
          size_t integer;

          while (precision--)
          {
          decimal *= 100.;
          integer = (int)decimal;


          could usefully be rewritten as



          static void print_decimal(double decimal, int precision)
          {
          while (precision--) {
          decimal *= 100.;
          size_t integer = (int)decimal;


          This spots a bug: you fail to handle negative inputs. If you want integer to hold a signed int, you should define it as int (not size_t); and vice versa, if you want it to hold an unsigned size_t, you should be casting (size_t)decimal (not (int)decimal).





              if (!precision  && (integer % 10) >= 5 && decimal != (double)integer)
          integer += 10;


          This line could use some comments. I imagine it has something to do with rounding?





              printf("%i", integer);


          It's more idiomatic to use printf("%d", integer) to print a decimal integer in C (and C++). True, %i exists, but it's deservedly obscure.



          (Also, because of the above bug where you made integer a size_t, down here you should be using %zu to print it. But once you make it an int, you can go back to using %d.)



          Every compiler will warn you about the format-string mismatch between %i and size_t. Are you compiling with -Wall? (No.) Why not? (There's no reason not to.) So, start compiling with -Wall today! It'll find your bugs so that you don't have to.



          Performance-wise... I conclude from context that you expect this line to only ever print one digit, is that right? That is, your code could benefit from some "design by contract":



              assert(0 <= integer && integer <= 9);
          printf("%i", integer);


          If the assertion is correct, then we can speed up the code by writing simply



              assert(0 <= integer && integer <= 9);
          putchar('0' + integer);


          However, by adding the assertion, we have made the code a little bit testable. We can write some simple tests — just loop over all the floating-point numbers in the world and see if any of them fail the assertion — and indeed it doesn't take long to find one that fails.



          print_decimal(0.097, 2);


          This prints



          010


          when it should actually have printed



          10


          according to my understanding of your intent.





          Which reminds me: print_decimal is a weird name for this function, since it literally does not have the ability to print a decimal point! Even your example print_decimal(65.226, 4) actually prints 652260, not 65.2260. This seems problematic for your prospective users.





          So in conclusion:




          • Make your code testable.

          • Test your code.

          • Compile your code with -Wall.






          share|improve this answer









          $endgroup$





















            0












            $begingroup$

            Limited range



            double decimal, ... integer = (int)decimal; is undefined behavior for well over 90% of all double as the value is well outside the int range of INT_MIN...INT_MAX.



            To truncate a double, use double trunc(double).



            Imprecision



            decimal *= 100. is not certainly exact. With corner cases near xxxx.xx5, OP's approach to generate a nearby, though wrong answer.



            Negative numbers error



            print_decimal(-65.226, 4); --> -1717987571-1932735284-1932735284-1932735283



            Code is buggy



            print_decimal(65.9999999999999, 4); --> 6599910. This is due to the final rounding not accounted for.





            to be more effective



            Avoid range errors and those due to multiple rounding.



            To do this well is a non-trivial problem and requires a fair amount of code. A suitable short answer is possible if the range of double allowed (OP unfortunately has not supplied a restricted range) is reduced or sprintf() is callable.



            Below is some quick code. It too has imprecision issues noted about, but less so than OP's. Also OP's various failing cases are corrected here.



            #include <limits.h>
            #include <math.h>
            #include <stdint.h>

            static void print10(long double ldecimal) {
            if (ldecimal >= 10.0) {
            print10(ldecimal/10);
            }
            ldouble mod10 = fmodl(ldecimal, 10);
            putchar((int) mod10 + '0');
            }

            static void print_decimal2(double decimal, unsigned precision) {
            if (!isfinite(decimal)) {
            return ; // TBD_Code();
            }

            if (signbit(decimal)) {
            putchar('-');
            decimal = -decimal;
            }

            double ipart;
            double fpart = modf(decimal, &ipart);
            // By noting if there is a non-zero fraction, then this block only needs to
            // handle `double` typically in the [0 ... 2^53] range
            if (fpart) {
            long double pow10 = powl(10, precision);
            ldecimal = roundl(decimal * pow10); // use extended precision as able
            print10(ldecimal);
            } else if (decimal) {
            print10(decimal);
            while (precision) {
            precision--;
            putchar('0');
            }
            } else {
            putchar('0');
            }
            }





            share|improve this answer











            $endgroup$













              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "196"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212415%2fprint-a-double-as-a-decimal-with-a-specified-precision%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0












              $begingroup$

              Well, there's the obvious stuff, like whitespace and declaring variables at their first point of use. For example:



              static void     print_decimal(double decimal, int precision)
              {
              size_t integer;

              while (precision--)
              {
              decimal *= 100.;
              integer = (int)decimal;


              could usefully be rewritten as



              static void print_decimal(double decimal, int precision)
              {
              while (precision--) {
              decimal *= 100.;
              size_t integer = (int)decimal;


              This spots a bug: you fail to handle negative inputs. If you want integer to hold a signed int, you should define it as int (not size_t); and vice versa, if you want it to hold an unsigned size_t, you should be casting (size_t)decimal (not (int)decimal).





                  if (!precision  && (integer % 10) >= 5 && decimal != (double)integer)
              integer += 10;


              This line could use some comments. I imagine it has something to do with rounding?





                  printf("%i", integer);


              It's more idiomatic to use printf("%d", integer) to print a decimal integer in C (and C++). True, %i exists, but it's deservedly obscure.



              (Also, because of the above bug where you made integer a size_t, down here you should be using %zu to print it. But once you make it an int, you can go back to using %d.)



              Every compiler will warn you about the format-string mismatch between %i and size_t. Are you compiling with -Wall? (No.) Why not? (There's no reason not to.) So, start compiling with -Wall today! It'll find your bugs so that you don't have to.



              Performance-wise... I conclude from context that you expect this line to only ever print one digit, is that right? That is, your code could benefit from some "design by contract":



                  assert(0 <= integer && integer <= 9);
              printf("%i", integer);


              If the assertion is correct, then we can speed up the code by writing simply



                  assert(0 <= integer && integer <= 9);
              putchar('0' + integer);


              However, by adding the assertion, we have made the code a little bit testable. We can write some simple tests — just loop over all the floating-point numbers in the world and see if any of them fail the assertion — and indeed it doesn't take long to find one that fails.



              print_decimal(0.097, 2);


              This prints



              010


              when it should actually have printed



              10


              according to my understanding of your intent.





              Which reminds me: print_decimal is a weird name for this function, since it literally does not have the ability to print a decimal point! Even your example print_decimal(65.226, 4) actually prints 652260, not 65.2260. This seems problematic for your prospective users.





              So in conclusion:




              • Make your code testable.

              • Test your code.

              • Compile your code with -Wall.






              share|improve this answer









              $endgroup$


















                0












                $begingroup$

                Well, there's the obvious stuff, like whitespace and declaring variables at their first point of use. For example:



                static void     print_decimal(double decimal, int precision)
                {
                size_t integer;

                while (precision--)
                {
                decimal *= 100.;
                integer = (int)decimal;


                could usefully be rewritten as



                static void print_decimal(double decimal, int precision)
                {
                while (precision--) {
                decimal *= 100.;
                size_t integer = (int)decimal;


                This spots a bug: you fail to handle negative inputs. If you want integer to hold a signed int, you should define it as int (not size_t); and vice versa, if you want it to hold an unsigned size_t, you should be casting (size_t)decimal (not (int)decimal).





                    if (!precision  && (integer % 10) >= 5 && decimal != (double)integer)
                integer += 10;


                This line could use some comments. I imagine it has something to do with rounding?





                    printf("%i", integer);


                It's more idiomatic to use printf("%d", integer) to print a decimal integer in C (and C++). True, %i exists, but it's deservedly obscure.



                (Also, because of the above bug where you made integer a size_t, down here you should be using %zu to print it. But once you make it an int, you can go back to using %d.)



                Every compiler will warn you about the format-string mismatch between %i and size_t. Are you compiling with -Wall? (No.) Why not? (There's no reason not to.) So, start compiling with -Wall today! It'll find your bugs so that you don't have to.



                Performance-wise... I conclude from context that you expect this line to only ever print one digit, is that right? That is, your code could benefit from some "design by contract":



                    assert(0 <= integer && integer <= 9);
                printf("%i", integer);


                If the assertion is correct, then we can speed up the code by writing simply



                    assert(0 <= integer && integer <= 9);
                putchar('0' + integer);


                However, by adding the assertion, we have made the code a little bit testable. We can write some simple tests — just loop over all the floating-point numbers in the world and see if any of them fail the assertion — and indeed it doesn't take long to find one that fails.



                print_decimal(0.097, 2);


                This prints



                010


                when it should actually have printed



                10


                according to my understanding of your intent.





                Which reminds me: print_decimal is a weird name for this function, since it literally does not have the ability to print a decimal point! Even your example print_decimal(65.226, 4) actually prints 652260, not 65.2260. This seems problematic for your prospective users.





                So in conclusion:




                • Make your code testable.

                • Test your code.

                • Compile your code with -Wall.






                share|improve this answer









                $endgroup$
















                  0












                  0








                  0





                  $begingroup$

                  Well, there's the obvious stuff, like whitespace and declaring variables at their first point of use. For example:



                  static void     print_decimal(double decimal, int precision)
                  {
                  size_t integer;

                  while (precision--)
                  {
                  decimal *= 100.;
                  integer = (int)decimal;


                  could usefully be rewritten as



                  static void print_decimal(double decimal, int precision)
                  {
                  while (precision--) {
                  decimal *= 100.;
                  size_t integer = (int)decimal;


                  This spots a bug: you fail to handle negative inputs. If you want integer to hold a signed int, you should define it as int (not size_t); and vice versa, if you want it to hold an unsigned size_t, you should be casting (size_t)decimal (not (int)decimal).





                      if (!precision  && (integer % 10) >= 5 && decimal != (double)integer)
                  integer += 10;


                  This line could use some comments. I imagine it has something to do with rounding?





                      printf("%i", integer);


                  It's more idiomatic to use printf("%d", integer) to print a decimal integer in C (and C++). True, %i exists, but it's deservedly obscure.



                  (Also, because of the above bug where you made integer a size_t, down here you should be using %zu to print it. But once you make it an int, you can go back to using %d.)



                  Every compiler will warn you about the format-string mismatch between %i and size_t. Are you compiling with -Wall? (No.) Why not? (There's no reason not to.) So, start compiling with -Wall today! It'll find your bugs so that you don't have to.



                  Performance-wise... I conclude from context that you expect this line to only ever print one digit, is that right? That is, your code could benefit from some "design by contract":



                      assert(0 <= integer && integer <= 9);
                  printf("%i", integer);


                  If the assertion is correct, then we can speed up the code by writing simply



                      assert(0 <= integer && integer <= 9);
                  putchar('0' + integer);


                  However, by adding the assertion, we have made the code a little bit testable. We can write some simple tests — just loop over all the floating-point numbers in the world and see if any of them fail the assertion — and indeed it doesn't take long to find one that fails.



                  print_decimal(0.097, 2);


                  This prints



                  010


                  when it should actually have printed



                  10


                  according to my understanding of your intent.





                  Which reminds me: print_decimal is a weird name for this function, since it literally does not have the ability to print a decimal point! Even your example print_decimal(65.226, 4) actually prints 652260, not 65.2260. This seems problematic for your prospective users.





                  So in conclusion:




                  • Make your code testable.

                  • Test your code.

                  • Compile your code with -Wall.






                  share|improve this answer









                  $endgroup$



                  Well, there's the obvious stuff, like whitespace and declaring variables at their first point of use. For example:



                  static void     print_decimal(double decimal, int precision)
                  {
                  size_t integer;

                  while (precision--)
                  {
                  decimal *= 100.;
                  integer = (int)decimal;


                  could usefully be rewritten as



                  static void print_decimal(double decimal, int precision)
                  {
                  while (precision--) {
                  decimal *= 100.;
                  size_t integer = (int)decimal;


                  This spots a bug: you fail to handle negative inputs. If you want integer to hold a signed int, you should define it as int (not size_t); and vice versa, if you want it to hold an unsigned size_t, you should be casting (size_t)decimal (not (int)decimal).





                      if (!precision  && (integer % 10) >= 5 && decimal != (double)integer)
                  integer += 10;


                  This line could use some comments. I imagine it has something to do with rounding?





                      printf("%i", integer);


                  It's more idiomatic to use printf("%d", integer) to print a decimal integer in C (and C++). True, %i exists, but it's deservedly obscure.



                  (Also, because of the above bug where you made integer a size_t, down here you should be using %zu to print it. But once you make it an int, you can go back to using %d.)



                  Every compiler will warn you about the format-string mismatch between %i and size_t. Are you compiling with -Wall? (No.) Why not? (There's no reason not to.) So, start compiling with -Wall today! It'll find your bugs so that you don't have to.



                  Performance-wise... I conclude from context that you expect this line to only ever print one digit, is that right? That is, your code could benefit from some "design by contract":



                      assert(0 <= integer && integer <= 9);
                  printf("%i", integer);


                  If the assertion is correct, then we can speed up the code by writing simply



                      assert(0 <= integer && integer <= 9);
                  putchar('0' + integer);


                  However, by adding the assertion, we have made the code a little bit testable. We can write some simple tests — just loop over all the floating-point numbers in the world and see if any of them fail the assertion — and indeed it doesn't take long to find one that fails.



                  print_decimal(0.097, 2);


                  This prints



                  010


                  when it should actually have printed



                  10


                  according to my understanding of your intent.





                  Which reminds me: print_decimal is a weird name for this function, since it literally does not have the ability to print a decimal point! Even your example print_decimal(65.226, 4) actually prints 652260, not 65.2260. This seems problematic for your prospective users.





                  So in conclusion:




                  • Make your code testable.

                  • Test your code.

                  • Compile your code with -Wall.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 6 hours ago









                  QuuxplusoneQuuxplusone

                  12.1k12061




                  12.1k12061

























                      0












                      $begingroup$

                      Limited range



                      double decimal, ... integer = (int)decimal; is undefined behavior for well over 90% of all double as the value is well outside the int range of INT_MIN...INT_MAX.



                      To truncate a double, use double trunc(double).



                      Imprecision



                      decimal *= 100. is not certainly exact. With corner cases near xxxx.xx5, OP's approach to generate a nearby, though wrong answer.



                      Negative numbers error



                      print_decimal(-65.226, 4); --> -1717987571-1932735284-1932735284-1932735283



                      Code is buggy



                      print_decimal(65.9999999999999, 4); --> 6599910. This is due to the final rounding not accounted for.





                      to be more effective



                      Avoid range errors and those due to multiple rounding.



                      To do this well is a non-trivial problem and requires a fair amount of code. A suitable short answer is possible if the range of double allowed (OP unfortunately has not supplied a restricted range) is reduced or sprintf() is callable.



                      Below is some quick code. It too has imprecision issues noted about, but less so than OP's. Also OP's various failing cases are corrected here.



                      #include <limits.h>
                      #include <math.h>
                      #include <stdint.h>

                      static void print10(long double ldecimal) {
                      if (ldecimal >= 10.0) {
                      print10(ldecimal/10);
                      }
                      ldouble mod10 = fmodl(ldecimal, 10);
                      putchar((int) mod10 + '0');
                      }

                      static void print_decimal2(double decimal, unsigned precision) {
                      if (!isfinite(decimal)) {
                      return ; // TBD_Code();
                      }

                      if (signbit(decimal)) {
                      putchar('-');
                      decimal = -decimal;
                      }

                      double ipart;
                      double fpart = modf(decimal, &ipart);
                      // By noting if there is a non-zero fraction, then this block only needs to
                      // handle `double` typically in the [0 ... 2^53] range
                      if (fpart) {
                      long double pow10 = powl(10, precision);
                      ldecimal = roundl(decimal * pow10); // use extended precision as able
                      print10(ldecimal);
                      } else if (decimal) {
                      print10(decimal);
                      while (precision) {
                      precision--;
                      putchar('0');
                      }
                      } else {
                      putchar('0');
                      }
                      }





                      share|improve this answer











                      $endgroup$


















                        0












                        $begingroup$

                        Limited range



                        double decimal, ... integer = (int)decimal; is undefined behavior for well over 90% of all double as the value is well outside the int range of INT_MIN...INT_MAX.



                        To truncate a double, use double trunc(double).



                        Imprecision



                        decimal *= 100. is not certainly exact. With corner cases near xxxx.xx5, OP's approach to generate a nearby, though wrong answer.



                        Negative numbers error



                        print_decimal(-65.226, 4); --> -1717987571-1932735284-1932735284-1932735283



                        Code is buggy



                        print_decimal(65.9999999999999, 4); --> 6599910. This is due to the final rounding not accounted for.





                        to be more effective



                        Avoid range errors and those due to multiple rounding.



                        To do this well is a non-trivial problem and requires a fair amount of code. A suitable short answer is possible if the range of double allowed (OP unfortunately has not supplied a restricted range) is reduced or sprintf() is callable.



                        Below is some quick code. It too has imprecision issues noted about, but less so than OP's. Also OP's various failing cases are corrected here.



                        #include <limits.h>
                        #include <math.h>
                        #include <stdint.h>

                        static void print10(long double ldecimal) {
                        if (ldecimal >= 10.0) {
                        print10(ldecimal/10);
                        }
                        ldouble mod10 = fmodl(ldecimal, 10);
                        putchar((int) mod10 + '0');
                        }

                        static void print_decimal2(double decimal, unsigned precision) {
                        if (!isfinite(decimal)) {
                        return ; // TBD_Code();
                        }

                        if (signbit(decimal)) {
                        putchar('-');
                        decimal = -decimal;
                        }

                        double ipart;
                        double fpart = modf(decimal, &ipart);
                        // By noting if there is a non-zero fraction, then this block only needs to
                        // handle `double` typically in the [0 ... 2^53] range
                        if (fpart) {
                        long double pow10 = powl(10, precision);
                        ldecimal = roundl(decimal * pow10); // use extended precision as able
                        print10(ldecimal);
                        } else if (decimal) {
                        print10(decimal);
                        while (precision) {
                        precision--;
                        putchar('0');
                        }
                        } else {
                        putchar('0');
                        }
                        }





                        share|improve this answer











                        $endgroup$
















                          0












                          0








                          0





                          $begingroup$

                          Limited range



                          double decimal, ... integer = (int)decimal; is undefined behavior for well over 90% of all double as the value is well outside the int range of INT_MIN...INT_MAX.



                          To truncate a double, use double trunc(double).



                          Imprecision



                          decimal *= 100. is not certainly exact. With corner cases near xxxx.xx5, OP's approach to generate a nearby, though wrong answer.



                          Negative numbers error



                          print_decimal(-65.226, 4); --> -1717987571-1932735284-1932735284-1932735283



                          Code is buggy



                          print_decimal(65.9999999999999, 4); --> 6599910. This is due to the final rounding not accounted for.





                          to be more effective



                          Avoid range errors and those due to multiple rounding.



                          To do this well is a non-trivial problem and requires a fair amount of code. A suitable short answer is possible if the range of double allowed (OP unfortunately has not supplied a restricted range) is reduced or sprintf() is callable.



                          Below is some quick code. It too has imprecision issues noted about, but less so than OP's. Also OP's various failing cases are corrected here.



                          #include <limits.h>
                          #include <math.h>
                          #include <stdint.h>

                          static void print10(long double ldecimal) {
                          if (ldecimal >= 10.0) {
                          print10(ldecimal/10);
                          }
                          ldouble mod10 = fmodl(ldecimal, 10);
                          putchar((int) mod10 + '0');
                          }

                          static void print_decimal2(double decimal, unsigned precision) {
                          if (!isfinite(decimal)) {
                          return ; // TBD_Code();
                          }

                          if (signbit(decimal)) {
                          putchar('-');
                          decimal = -decimal;
                          }

                          double ipart;
                          double fpart = modf(decimal, &ipart);
                          // By noting if there is a non-zero fraction, then this block only needs to
                          // handle `double` typically in the [0 ... 2^53] range
                          if (fpart) {
                          long double pow10 = powl(10, precision);
                          ldecimal = roundl(decimal * pow10); // use extended precision as able
                          print10(ldecimal);
                          } else if (decimal) {
                          print10(decimal);
                          while (precision) {
                          precision--;
                          putchar('0');
                          }
                          } else {
                          putchar('0');
                          }
                          }





                          share|improve this answer











                          $endgroup$



                          Limited range



                          double decimal, ... integer = (int)decimal; is undefined behavior for well over 90% of all double as the value is well outside the int range of INT_MIN...INT_MAX.



                          To truncate a double, use double trunc(double).



                          Imprecision



                          decimal *= 100. is not certainly exact. With corner cases near xxxx.xx5, OP's approach to generate a nearby, though wrong answer.



                          Negative numbers error



                          print_decimal(-65.226, 4); --> -1717987571-1932735284-1932735284-1932735283



                          Code is buggy



                          print_decimal(65.9999999999999, 4); --> 6599910. This is due to the final rounding not accounted for.





                          to be more effective



                          Avoid range errors and those due to multiple rounding.



                          To do this well is a non-trivial problem and requires a fair amount of code. A suitable short answer is possible if the range of double allowed (OP unfortunately has not supplied a restricted range) is reduced or sprintf() is callable.



                          Below is some quick code. It too has imprecision issues noted about, but less so than OP's. Also OP's various failing cases are corrected here.



                          #include <limits.h>
                          #include <math.h>
                          #include <stdint.h>

                          static void print10(long double ldecimal) {
                          if (ldecimal >= 10.0) {
                          print10(ldecimal/10);
                          }
                          ldouble mod10 = fmodl(ldecimal, 10);
                          putchar((int) mod10 + '0');
                          }

                          static void print_decimal2(double decimal, unsigned precision) {
                          if (!isfinite(decimal)) {
                          return ; // TBD_Code();
                          }

                          if (signbit(decimal)) {
                          putchar('-');
                          decimal = -decimal;
                          }

                          double ipart;
                          double fpart = modf(decimal, &ipart);
                          // By noting if there is a non-zero fraction, then this block only needs to
                          // handle `double` typically in the [0 ... 2^53] range
                          if (fpart) {
                          long double pow10 = powl(10, precision);
                          ldecimal = roundl(decimal * pow10); // use extended precision as able
                          print10(ldecimal);
                          } else if (decimal) {
                          print10(decimal);
                          while (precision) {
                          precision--;
                          putchar('0');
                          }
                          } else {
                          putchar('0');
                          }
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 5 hours ago

























                          answered 6 hours ago









                          chuxchux

                          13.1k21344




                          13.1k21344






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Code Review Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212415%2fprint-a-double-as-a-decimal-with-a-specified-precision%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to make a Squid Proxy server?

                              Is this a new Fibonacci Identity?

                              19世紀