How to convert float to string












0















I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.



For ex: 
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?









share|improve this question




















  • 2





    Possible duplicate of How to format floating point numbers into a string using Go

    – nilsocket
    Nov 15 '18 at 6:05
















0















I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.



For ex: 
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?









share|improve this question




















  • 2





    Possible duplicate of How to format floating point numbers into a string using Go

    – nilsocket
    Nov 15 '18 at 6:05














0












0








0








I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.



For ex: 
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?









share|improve this question
















I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.



For ex: 
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?






string go floating-point precision






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:24









Flimzy

38.9k106597




38.9k106597










asked Nov 15 '18 at 5:12









Nagireddy HanishaNagireddy Hanisha

328525




328525








  • 2





    Possible duplicate of How to format floating point numbers into a string using Go

    – nilsocket
    Nov 15 '18 at 6:05














  • 2





    Possible duplicate of How to format floating point numbers into a string using Go

    – nilsocket
    Nov 15 '18 at 6:05








2




2





Possible duplicate of How to format floating point numbers into a string using Go

– nilsocket
Nov 15 '18 at 6:05





Possible duplicate of How to format floating point numbers into a string using Go

– nilsocket
Nov 15 '18 at 6:05












2 Answers
2






active

oldest

votes


















3














Use strconv.FormatFloat like such:



s := strconv.FormatFloat(3.1415, 'E', -1, 64)
fmt.Println(s)


Outputs




3.1415







share|improve this answer

































    1














    Convert float to string



    FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).




    func FormatFloat(f float64, fmt byte, prec, bitSize int) string




    f := 3.14159265
    s := strconv.FormatFloat(f, 'E', -1, 64)
    fmt.Println(s)



    Output is "3.14159265"




    Another method is by using fmt.Sprintf



    s := fmt.Sprintf("%f", 123.456) 
    fmt.Println(s)



    Output is "123.456000"




    Check the code on play ground






    share|improve this answer

























      Your Answer






      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: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53312828%2fhow-to-convert-float-to-string%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









      3














      Use strconv.FormatFloat like such:



      s := strconv.FormatFloat(3.1415, 'E', -1, 64)
      fmt.Println(s)


      Outputs




      3.1415







      share|improve this answer






























        3














        Use strconv.FormatFloat like such:



        s := strconv.FormatFloat(3.1415, 'E', -1, 64)
        fmt.Println(s)


        Outputs




        3.1415







        share|improve this answer




























          3












          3








          3







          Use strconv.FormatFloat like such:



          s := strconv.FormatFloat(3.1415, 'E', -1, 64)
          fmt.Println(s)


          Outputs




          3.1415







          share|improve this answer















          Use strconv.FormatFloat like such:



          s := strconv.FormatFloat(3.1415, 'E', -1, 64)
          fmt.Println(s)


          Outputs




          3.1415








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 14:21

























          answered Nov 15 '18 at 5:28









          UllaakutUllaakut

          1,502714




          1,502714

























              1














              Convert float to string



              FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).




              func FormatFloat(f float64, fmt byte, prec, bitSize int) string




              f := 3.14159265
              s := strconv.FormatFloat(f, 'E', -1, 64)
              fmt.Println(s)



              Output is "3.14159265"




              Another method is by using fmt.Sprintf



              s := fmt.Sprintf("%f", 123.456) 
              fmt.Println(s)



              Output is "123.456000"




              Check the code on play ground






              share|improve this answer






























                1














                Convert float to string



                FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).




                func FormatFloat(f float64, fmt byte, prec, bitSize int) string




                f := 3.14159265
                s := strconv.FormatFloat(f, 'E', -1, 64)
                fmt.Println(s)



                Output is "3.14159265"




                Another method is by using fmt.Sprintf



                s := fmt.Sprintf("%f", 123.456) 
                fmt.Println(s)



                Output is "123.456000"




                Check the code on play ground






                share|improve this answer




























                  1












                  1








                  1







                  Convert float to string



                  FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).




                  func FormatFloat(f float64, fmt byte, prec, bitSize int) string




                  f := 3.14159265
                  s := strconv.FormatFloat(f, 'E', -1, 64)
                  fmt.Println(s)



                  Output is "3.14159265"




                  Another method is by using fmt.Sprintf



                  s := fmt.Sprintf("%f", 123.456) 
                  fmt.Println(s)



                  Output is "123.456000"




                  Check the code on play ground






                  share|improve this answer















                  Convert float to string



                  FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).




                  func FormatFloat(f float64, fmt byte, prec, bitSize int) string




                  f := 3.14159265
                  s := strconv.FormatFloat(f, 'E', -1, 64)
                  fmt.Println(s)



                  Output is "3.14159265"




                  Another method is by using fmt.Sprintf



                  s := fmt.Sprintf("%f", 123.456) 
                  fmt.Println(s)



                  Output is "123.456000"




                  Check the code on play ground







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 9:01

























                  answered Nov 15 '18 at 7:23









                  ASHWIN RAJEEVASHWIN RAJEEV

                  241211




                  241211






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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.


                      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%2fstackoverflow.com%2fquestions%2f53312828%2fhow-to-convert-float-to-string%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python