Author Topic: FYI - Format reals to strings with ToString method  (Read 3398 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
FYI - Format reals to strings with ToString method
« on: September 19, 2008, 03:47:45 PM »
I never knew that you could tell it how many decimal places you wanted it to show.  I have used 'ToString', but always typed it so fast that I didn't see you could format it.  So I was thinking that I was going to have to write a format function for some reals, so that they will show all the decimals I want.  So incase you haven't used the format options on 'ToString', it goes like

real = 7.00
real.ToString() = "7"
real.ToString( "0.00" ) = "7.00"

real = 7.05
real.ToString() = "7.05"
real.ToString( "0.0" ) = "7.1"

Man I can't believe I missed that, but I don't think I ever had a use for it until now.

It must be Friday.....
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: FYI - Format reals to strings with ToString method
« Reply #1 on: September 19, 2008, 03:55:00 PM »
Friday...definately ;) (you used that one on me last Friday I believe in another thread).

Wait 'till you start getting into custom format specifiers.... :evil:

T.Willey

  • Needs a day job
  • Posts: 5251
Re: FYI - Format reals to strings with ToString method
« Reply #2 on: September 19, 2008, 04:02:51 PM »
Friday...definately ;) (you used that one on me last Friday I believe in another thread).
Yea, I love the Friday excuse.

Wait 'till you start getting into custom format specifiers.... :evil:
I looked at those before I knew I could format it with just the ToString call, but didn't feel like reading/understanding it today.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Spike Wilbury

  • Guest
Re: FYI - Format reals to strings with ToString method
« Reply #3 on: September 19, 2008, 08:46:29 PM »
I never knew that you could tell it how many decimal places you wanted it to show.  I have used 'ToString', but always typed it so fast that I didn't see you could format it.  So I was thinking that I was going to have to write a format function for some reals, so that they will show all the decimals I want.  So incase you haven't used the format options on 'ToString', it goes like

real = 7.00
real.ToString() = "7"
real.ToString( "0.00" ) = "7.00"

real = 7.05
real.ToString() = "7.05"
real.ToString( "0.0" ) = "7.1"

Man I can't believe I missed that, but I don't think I ever had a use for it until now.

It must be Friday.....


And you don't have to wait for the other next Friday, to know that you can use :)

Quote
Autodesk.AutoCAD.Runtime.DistanceToString(double, DistanceUnitFormat, int); // distance to format // Lunits // Precision

Example:
str_distanceCorner = editDistanceCorner.Text;
double d_distCorner = Converter.StringToDistance(str_distanceCorner, DistanceUnitFormat.Architectural);
editDistanceCorner.Text = Converter.DistanceToString(d_distCorner, DistanceUnitFormat.Architectural, -1); // where -1 is current Luprec value

sinc

  • Guest
Re: FYI - Format reals to strings with ToString method
« Reply #4 on: September 20, 2008, 11:33:06 AM »
Since the topic has come up, is there an easy way to specify the maximum digits after the decimal?  For example, if I set it to 2 decimal places, I would see the following:

value = display

1 = 1
1.5 = 1.5
1.52 = 1.52
1.5555 = 1.56
1.50 = 1.5
1.00 = 1

I wrote a little routine that strips the trailing zeros (and decimal point, if applicable) from the end of the input value, which was simple enough, but I was wondering if there was some built-in way to do this.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: FYI - Format reals to strings with ToString method
« Reply #5 on: September 20, 2008, 12:58:08 PM »
Tim's example does that
 double d = 121.500001;
 ed.WriteMessage("\n" + d.ToString("0.00"));

121.50

Bryco

  • Water Moccasin
  • Posts: 1883
Re: FYI - Format reals to strings with ToString method
« Reply #6 on: September 20, 2008, 03:23:30 PM »
well I didn't read your post well sinc but I think this is what you want
the 0.00 will leave the zeroes whereas 0.## wont.
double[]dNums=new double[] { 1,1.5, 1.52,1.5555,1.50,1.00 };
            foreach (double d in dNums)
            {
                ed.WriteMessage("\nFormat 0.00=" + d.ToString("0.00"));
                ed.WriteMessage("\nFormat 0.##=" + d.ToString("0.##"));
            }

Format 0.00=1.00
Format 0.##=1
Format 0.00=1.50
Format 0.##=1.5
Format 0.00=1.52
Format 0.##=1.52
Format 0.00=1.56
Format 0.##=1.56
Format 0.00=1.50
Format 0.##=1.5
Format 0.00=1.00
Format 0.##=1

sinc

  • Guest
Re: FYI - Format reals to strings with ToString method
« Reply #7 on: September 21, 2008, 09:25:55 AM »
Yeah, that's it.  Thanks!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: FYI - Format reals to strings with ToString method
« Reply #8 on: September 22, 2008, 11:07:46 AM »
Thanks Luis.  Didn't even look in the Acad stuff.  I like.  Too bad I can't go home already since I did learn something new already.


Nice solution Bryco.  I wouldn't have even thought of doing it that way.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.