<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Reduce decimal value to 4 digits in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970736#M37582</link>
    <description>&lt;P&gt;Hello everyone.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HAPPY NEW YEAR TO ALL OF YOU. &lt;img id="heart" class="emoticon emoticon-heart" src="https://forums.autodesk.com/i/smilies/16x16_heart.png" alt="Heart" title="Heart" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing lengths of lines into a single text , but the probem is that the value of lengths is many digits and I would like it to be 2 or 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how to reduce the decimal numbers to a specific number of digits?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks.&lt;/P&gt;</description>
    <pubDate>Sun, 03 Jan 2016 10:56:49 GMT</pubDate>
    <dc:creator>J-Rocks</dc:creator>
    <dc:date>2016-01-03T10:56:49Z</dc:date>
    <item>
      <title>Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970736#M37582</link>
      <description>&lt;P&gt;Hello everyone.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HAPPY NEW YEAR TO ALL OF YOU. &lt;img id="heart" class="emoticon emoticon-heart" src="https://forums.autodesk.com/i/smilies/16x16_heart.png" alt="Heart" title="Heart" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing lengths of lines into a single text , but the probem is that the value of lengths is many digits and I would like it to be 2 or 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how to reduce the decimal numbers to a specific number of digits?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks.&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2016 10:56:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970736#M37582</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-03T10:56:49Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970748#M37583</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to round the length double value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Math.Round(length, 4)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want the string to display 4 digits without changin the double value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;length.ToString("0.0000")&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Jan 2016 11:54:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970748#M37583</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-01-03T11:54:12Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970754#M37584</link>
      <description>&lt;P&gt;Thank you .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I needed the first answer. &lt;img id="smileywink" class="emoticon emoticon-smileywink" src="https://forums.autodesk.com/i/smilies/16x16_smiley-wink.png" alt="Smiley Wink" title="Smiley Wink" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am also using your polar method to get the rotation of lines to add single text to each line object, it seems it doesn't work. can you please advise on this ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if (ss.Status == PromptStatus.OK)
                {
                  foreach (SelectedObject obj in ss.Value)
                    {     
                            ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
                            len = ent.Length;
                            dis += len;
                            p1 = ent.StartPoint;
                            p2 = ent.EndPoint;
                            &lt;FONT color="#0000FF"&gt;p3 = Polar (p1,ent.Angle, (len / 2.0))// IT IS NOT AS PER LINE'S ROTATION&lt;/FONT&gt;
                            txt = new DBText();
                            txt.SetDatabaseDefaults();
                            &lt;FONT color="#000000"&gt;txt.Position = p3&lt;/FONT&gt;
                            txt.TextString = Math.Round(len, 4).ToString();
                            txt.Height = 0.2;
                            blkrec.AppendEntity(txt);
                            trans.AddNewlyCreatedDBObject(txt, true);    
                        }
                        trans.Commit();
                    }  &lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Jan 2016 12:07:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970754#M37584</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-03T12:07:13Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970887#M37585</link>
      <description>&lt;P&gt;I don't know why the Polar method isn't working (can you show the code you're using).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you can also simply get the middle of the line like so:&lt;/P&gt;
&lt;PRE&gt;p3 = p1 + (p2 - p1) / 2.0;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2016 18:08:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5970887#M37585</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-01-03T18:08:20Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971177#M37586</link>
      <description>&lt;P&gt;Hi gile&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the codes that I wrote and the rotation of text is always 0.0 and it is not following each line's angle.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have any comment about the codes please let me know.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;

namespace SecondExample
{
    public class Class1
    {
        [CommandMethod("LineLengths", CommandFlags.UsePickSet)]
        public void GetlineLengths()
        {
            Editor edit = Application.DocumentManager.MdiActiveDocument.Editor;
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database Dtb = acDoc.Database;
            using (Transaction trans = Dtb.TransactionManager.StartTransaction())
            {
                double len, dis = 0.0 ;
                Line ent;
                Point3d p1, p2, p3;
                DBText txt;
                try
                {
                BlockTable blktbl = trans.GetObject(Dtb.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord blkrec = trans.GetObject(Dtb.CurrentSpaceId, OpenMode.ForWrite)as BlockTableRecord;
                TypedValue[] tv = new TypedValue [] { new TypedValue(0, "LINE") };
                SelectionFilter fltr = new SelectionFilter(tv);
                PromptSelectionResult ss = edit.GetSelection(fltr);
                if (ss.Status == PromptStatus.OK)
                {
                  foreach (SelectedObject obj in ss.Value)
                    {     
                            ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
                            len = ent.Length;
                            dis += len;
                            p1 = ent.StartPoint;
                            p2 = ent.EndPoint;
                            p3 = Polar (p1,ent.Angle, (len / 2.0));
                            txt = new DBText();
                            txt.SetDatabaseDefaults();
                            txt.Position = p3;
                            txt.TextString = Math.Round(len, 4).ToString();
                            txt.Height = 0.2;
                            blkrec.AppendEntity(txt);
                            trans.AddNewlyCreatedDBObject(txt, true);    
                        }
                        trans.Commit();
                    }  
                if (dis &amp;gt; 0.0)
                    {
                        edit.WriteMessage("Total lengths : " + dis.ToString());
                    }
                }
                catch (System.Exception exp)
                {
                    edit.WriteMessage(exp.Message);
                    trans.Abort();
                }
            } 
        }
        /// &amp;lt;summary&amp;gt;
        /// Defines a point with polar coordinates from a base point.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="basePt"&amp;gt;The base point.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="angle"&amp;gt;The angle (radians) about the X axis.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="distance"&amp;gt;The distance from the base point.&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;The new 3d point.&amp;lt;/returns&amp;gt;
        public Point3d Polar(Point3d basePt, double angle, double distance)
        {
            return new Point3d(
                basePt.X + (distance * Math.Cos(angle)),
                basePt.Y + (distance * Math.Sin(angle)),
                basePt.Z);
        }     
    }
}

 &lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jan 2016 04:19:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971177#M37586</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-04T04:19:57Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971247#M37587</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You aren't actually setting the txt.Rotation.&lt;/P&gt;
&lt;P&gt;You will need something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;txt.Rotation = ent.Angle;&lt;/PRE&gt;
&lt;P&gt;You'll find this code does exactly what it's told ... unfortunately.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The next task will be to change the text rotation angle so that it's 'readable' from the right and bottom only ...&lt;/P&gt;
&lt;P&gt;if the line runs from right to left, so will the text &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need a helping hand, just yell.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2016 06:21:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971247#M37587</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2016-01-04T06:21:30Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971268#M37588</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kerry is right.&lt;/P&gt;
&lt;P&gt;The Polar function works as expect and have nothing to do with the text rotation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;                if (ss.Status == PromptStatus.OK)
                {
                    foreach (SelectedObject obj in ss.Value)
                    {
                        ent = (Line)trans.GetObject(obj.ObjectId, OpenMode.ForRead);
                        len = ent.Length;
                        dis += len;
                        p1 = ent.StartPoint;
                        p2 = ent.EndPoint;
                        p3 = p1 + (p2 - p1) / 2.0;
                        txt = new DBText();
                        txt.SetDatabaseDefaults();

                        // set the text position
                        txt.Position = p3;
                        // or set the text justification and position
                        //txt.Justify = AttachmentPoint.BottomCenter;
                        //txt.AlignmentPoint = p3;

                        // set the text rotation
                        var rot = ent.Angle;
                        if (Math.Cos(rot) &amp;lt; 0.0)
                            rot += Math.PI;
                        txt.Rotation = rot;

                        txt.TextString = Math.Round(len, 4).ToString();
                        txt.Height = 0.2;
                        blkrec.AppendEntity(txt);
                        trans.AddNewlyCreatedDBObject(txt, true);
                    }
                    trans.Commit();
                }&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jan 2016 06:54:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971268#M37588</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-01-04T06:54:46Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971279#M37589</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@KerryBrown wrote:&lt;BR /&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You aren't actually setting the txt.Rotation.&lt;/P&gt;&lt;P&gt;You will need something like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;txt.Rotation = ent.Angle;&lt;/PRE&gt;&lt;P&gt;You'll find this code does exactly what it's told ... unfortunately.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next task will be to change the text rotation angle so that it's 'readable' from the right and bottom only ...&lt;/P&gt;&lt;P&gt;if the line runs from right to left, so will the text &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you need a helping hand, just yell.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Awesome &lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One more thing about the rotation , some texts become in reverse direction ( from Right to Left ) &lt;img id="smileysurprised" class="emoticon emoticon-smileysurprised" src="https://forums.autodesk.com/i/smilies/16x16_smiley-surprised.png" alt="Smiley Surprised" title="Smiley Surprised" /&gt; and some on oblique lines become reversed also , need ro wrok on this problem if that is possible please.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2016 07:04:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971279#M37589</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-04T07:04:38Z</dc:date>
    </item>
    <item>
      <title>Re : Reduce decimal value to 4 digits</title>
      <link>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971293#M37590</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kerry is right.&lt;/P&gt;&lt;P&gt;The Polar function works as expect and have nothing to do with the text rotation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;                if (ss.Status == PromptStatus.OK)
                {
                    foreach (SelectedObject obj in ss.Value)
                    {
                        ent = (Line)trans.GetObject(obj.ObjectId, OpenMode.ForRead);
                        len = ent.Length;
                        dis += len;
                        p1 = ent.StartPoint;
                        p2 = ent.EndPoint;
                        p3 = p1 + (p2 - p1) / 2.0;
                        txt = new DBText();
                        txt.SetDatabaseDefaults();

                        // set the text position
                        txt.Position = p3;
                        // or set the text justification and position
                        //txt.Justify = AttachmentPoint.BottomCenter;
                        //txt.AlignmentPoint = p3;

                        // set the text rotation
                        var rot = ent.Angle;
                        if (Math.Cos(rot) &amp;lt; 0.0)
                            rot += Math.PI;
                        txt.Rotation = rot;

                        txt.TextString = Math.Round(len, 4).ToString();
                        txt.Height = 0.2;
                        blkrec.AppendEntity(txt);
                        trans.AddNewlyCreatedDBObject(txt, true);
                    }
                    trans.Commit();
                }&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You replied to my question before asking it &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt; how awesome guys you are!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is great now , I have learned a lot from this program .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much gile and Kerry.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2016 07:23:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reduce-decimal-value-to-4-digits/m-p/5971293#M37590</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-04T07:23:44Z</dc:date>
    </item>
  </channel>
</rss>

