Jump to content

Real number to string


Jéferson Gustavo

Recommended Posts

Hello everyone. I need your help again. Let's suppose that I have the real number 10000.34252 and I need to transform it into the following string "10.000,343" that is, add the "." to separate thousands, and leave it to 3 decimal places. The rtos even helps to leave with 3 decimal places, however when it comes to numbers with less than 3 decimal places, it does not add the leading zero. For example, when we apply (rtos 3.1000 2 3), it returns "3.1", when I wanted "3,100")
I even created a solution, but I would like to know if there is a cleaner way to do this.
Here is my solution:

  (setq number_str 10000.34252)

  (setq number_str (rtos number_str 2 3)
	number_str (vl-string-subst "," "." number_str ))

  (cond
    ((= number_str "0"))
    ((= (vl-string-search "," number_str) nil)(setq number_str (strcat number_str ",000")))
    ((= (- (strlen number_str) (vl-string-search "," number_str) 1) 1)(setq number_str (strcat number_str "00")))
    ((= (- (strlen number_str) (vl-string-search "," number_str) 1) 2)(setq number_str (strcat number_str "0"))))

  (if (> (vl-string-search "," number_str) 3)
    (setq number_str (strcat (substr number_str 1 (- (vl-string-search "," number_str) 3)) "." (substr number_str (- (vl-string-search "," number_str) 2)))))

This returns "10.000,343".

Thank you in advance

Link to comment
Share on other sites

If your drawing is metric, try setting the system variable "DIMZIN" to 0 (zero) . Settings 0->3 only affect feet and inches and in 2012 this doesn't suppress trailing zero's in metric drawings. You can test on the command line with (rtos num 2 3)

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...