Jump to content
Developer Wiki and Function Reference Links ×

How to removing trailing zeros?


Recommended Posts

I am working on a script that calculates the height and width of a surface area and delivers the results to the fourth decimal place.  Is there a way to remove trailing zeros from a number? If the result is 24.0625 then it shows 24.0625 as it should.  But if the result is 24.0000 then I only want to show 24.

Link to comment

Doesn't the Round function eliminate the precision I need to show non-whole numbers?

I need to show non-whole numbers to a maximum of 4 decimal places.  I only want to eliminate trailing zeros.

 

Examples:

Round(24.0000); { returns 24 } 

Round(24.0625); { returns 24 } { I need it to return 24.0625 }

Round (24.1250); { returns 24 } { I need it to return 24.125 }

Link to comment

Are you displaying as text? The easiest thing is to set your units setting the way you want your dimension string to be represented. You could also iterate through the text string from the back and remove any zeros. 
If this is a number, try multiplying by 10000, rounding, then dividing by 10000

Link to comment
7 minutes ago, JBenghiat said:

Are you displaying as text? The easiest thing is to set your units setting the way you want your dimension string to be represented. You could also iterate through the text string from the back and remove any zeros. 
If this is a number, try multiplying by 10000, rounding, then dividing by 10000

 

Thank you for your suggestions.  I am very new to VectorScript and in over my head on this so I apologize in advance.

Yes, I am displaying as text. I am interested in your suggestion to iterate through the text string from the back and remove any zeros.  I am guessing the process involves identifying the position of the decimal point in the string, then Iterate from the end of the string to find the first non-zero digit, and then trim the string to remove the trailing zeros. 

 

Link to comment
Posted (edited)


Hello @exhibitdesigner,
   Been there, done that. Like you, this bothers me, too. I wrote this about 10 years ago and it works well. It rounds off excess 0's as well as 9's.
  
    By excess 9's I mean when you have more 9's than you have display precision. Example, your display precision is 3 decimals, so "2.999" will display as "2.999", but "2.9999" (or anything >= 2.9995) will display as "3". This function tests your display precision, so you don't have to know in advance whether to multiply and divide by 10, 100, 1000, 10000, etc. 
  
Try using this function to tailor your numbers to your liking:

function TZ(R :Real) :Real;
{ Trailing Zeros (and 9's) suppressed - Round numbers to their display precision. }
{ 2 May 2024 - Raymond Mullin }
Var
	DP :Real;	{ drawing precision }
Begin
	DP := 10^GetPrefLongint(162);		{ = 1/DwgPrec }
	TZ := Round(R * DP) / DP;
End;		{ TZ }


To use, replace this:
   Message('The ratio is ', aReal);
  
with this:
   Message('The ratio is ', TZ(aReal));

 

HTH,

Raymond

Edited by MullinRJ
  • Like 2
Link to comment

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
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...