Convert String to float and still retain the precision

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tumblypooh
    New Member
    • Oct 2008
    • 3

    Convert String to float and still retain the precision

    I am trying to write a method that have this signature in C#
    string ConvertFloatToS tring(float floatNumber, int precision)

    if we call ConvertFloatToS tring (1234.3433,2) then the method should return "1234.34"

    Can give me some idea?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by tumblypooh
    I am trying to write a method that have this signature in C#
    string ConvertFloatToS tring(float floatNumber, int precision)

    if we call ConvertFloatToS tring (1234.3433,2) then the method should return "1234.34"

    Can give me some idea?
    This is built into a float type. Use the .ToString() method
    Code:
                float bob = 123.456f;
                string szBob = bob.ToString("0.00");
                MessageBox.Show(szBob);
    Other format samples
    iFormatProvider example

    Comment

    • tumblypooh
      New Member
      • Oct 2008
      • 3

      #3
      Originally posted by tlhintoq
      This is built into a float type. Use the .ToString() method
      Code:
                  float bob = 123.456f;
                  string szBob = bob.ToString("0.00");
                  MessageBox.Show(szBob);
      Other format samples
      iFormatProvider example
      We can use the Convert.ToStrin g() which is the built-in function, we need to invet our own algorithm to implement the method. :(
      any thought?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        This must be a home work assignment type thing then?
        Please read the posting guidelines, which covers why we do not answer homework questions

        Comment

        • tumblypooh
          New Member
          • Oct 2008
          • 3

          #5
          Originally posted by Plater
          This must be a home work assignment type thing then?
          Please read the posting guidelines, which covers why we do not answer homework questions
          This is not a homework assignment as I am not studying but I am a kind of person who is interested in doing programming challenging problem in terms of efficiency.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            You are not going to get more effecient then the .ToString() method without using unmanaged code.

            Comment

            Working...