Get Precision for Text Value

Get Precision for Text Value

traiduong014969
Collaborator Collaborator
444 Views
3 Replies
Message 1 of 4

Get Precision for Text Value

traiduong014969
Collaborator
Collaborator

Hi all, 

Recently, I how to learn get Coordinate value from click by mouse. However, the value that i receive has many number behind "." . So I want to get a number that precision is ".00". For example Point 1 (5.23, 6.45, 5,77)

NOT ((5.23434645, 6.45463463456, 5.77xxx-xxxxxxxx). You can see the snapshot below:

traiduong014969_0-1675012316665.png

 

Someone can help me edit again this code

Thank you !

[CommandMethod("11GetPoint")]
        public void 11GetPoint()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            PromptPointOptions pr = new PromptPointOptions("Click mouse to get point");
            pr.AllowNone = true;
            pr.AllowArbitraryInput = true;   
            PromptPointResult kq = ed.GetPoint(pr);

            if (kq.Status != PromptStatus.OK) return;
            pr.BasePoint = kq.Value;
            pr.UseBasePoint = true;
            
            PromptResult kq1 = ed.GetPoint(pr);
        
            if (kq1.Status != PromptStatus.OK) return;
            
            ed.WriteMessage("\np1 = " + kq.Value.ToString() + "p2 = " + kq1.ToString());


        }

 

0 Likes
Accepted solutions (2)
445 Views
3 Replies
Replies (3)
Message 2 of 4

Jeff_M
Consultant
Consultant
Accepted solution

Use the Fixed-point formatting: ToString("F2")

See THIS for more on string formatting.

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

The Point3d structure implements IFormatable so you can pass a format to the ToString method (this requires to also pass a FormatProvider)

 

ed.WriteMessage("\np1 = " 
    + kq.Value.ToString("0.00", CultureInfo.CurrentCulture) 
    + "p2 = " 
    + kq1.Value.ToString("0.00", CultureInfo.CurrentCulture));

 

This can be simplified using the overload of Editor.WriteMessage method which takes a params object[] argument(s).

 

ed.WriteMessage("\np1 = {0:0.00} p2 = {1:0.00}", kq.Value, kq1.Value);

 

Or the (not so) new string interpolation using $.

 

ed.WriteMessage($"\np1 = {kq.Value:0.00} p2 = {kq1.Value:0.00}");

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

traiduong014969
Collaborator
Collaborator

Thanh you @_gile@Jeff_M .

This working fine.

0 Likes