Author Topic: Issue with the Round() function  (Read 804 times)

jwillard

  • Newbie
  • *
  • Posts: 12
Issue with the Round() function
« on: December 30, 2017, 08:38:32 AM »
Hey guys, I'm having an issue with the Round() statement not appearing to work, at least not as I expect. I've been trying to use a For next loop counter variable as a whole integer but it keeps returning to 3 decimal places. Granted, they're zero's but, All I want is the integer.
Ultimately I'm converting it into a string and i could play text string games with it and strip out the decimal and the zeros but that seems like a lot of code for something that should be a simple built in function

Window 10, 30 'Open a text window 10 lines x 30 characters.

For j = 1 To 6 'Print numbers 1-6 in the window.
Print Round(j)

Next j
Anykey

Any thoughts?
Jeff

jwillard

  • Newbie
  • *
  • Posts: 12
Re: Issue with the Round() function
« Reply #1 on: December 30, 2017, 11:25:11 AM »
OK... Now this is getting weird. I'm also having issue with some of the string functions. I cut and pasted the example out of the documentation for the MKS$() function and it's returning some weird stuff.
Window 10, 30 'Open a text window 10 lines x 30 characters.
On Error goto ErrorHandler
x = 1.111
      a$ = MKS$(x) 'convert the number to a string
      print "[" + a$ + "]"
      anykey
      y = CVS(a$)   'convert the string back to a number
      print y
      anykey
End
ErrorHandler:
message "An Error Occured"
end

Here's the output...

Dr PR

  • Hero Member
  • *****
  • Posts: 5611
Re: Issue with the Round() function
« Reply #2 on: December 30, 2017, 03:43:30 PM »
What version of DesignCAD? What version of Windows? 32 or 64 bit?

Phil
DesignCAD user since 1987

Lar

  • Hero Member
  • *****
  • Posts: 2815
Re: Issue with the Round() function
« Reply #3 on: December 30, 2017, 04:58:16 PM »

For your starting post:
Add the following somewhere near the start of the code...
Code: [Select]
precision 0
That will get rid of the trailing zeros. The default precision is 3 (so this is used if you don't stipulate a precision), that's why you got them. Note that precision only affects how numbers are displayed, not the actual value.

For your 2nd post:
My understanding is that converting numbers to strings doesn't write it out in words. It presents it in some computer language (an "8-character string"), as shown in your image.

To convert a string to numeric values use val(a$). It doesn't convert words to numbers but it can evaluate symbols, like foot and inches (' and ") and those for degree-minute-second (eg 1D1M1S, ie, 1 degree, 1 minute, 1 second). So if your code says a$="1' " (I put a space between the foot symbol and the ending quotes, for clarity) then val(a$) equals 1 if using foot based units, 12 if using inch based units, 30.480 if using cm based units (and the default precision of 3 - note that when converting string to values the current precision can affect the actual value), etc. (use sys(1100) to set the current drawing units in a macro [0 = unitless, 1 = inches, 2 = feet, 3 = miles, 4 = mm, 5 = cm, 6 = m, 7 = km]).

To convert a number back into a sting just assign it (eg, a$=a  <<note that a would have been assigned a numeric value earlier in the code>>). This would not convert the number into words but would make the number usable in text strings, as opposed to usable in calculations.


Lar
« Last Edit: December 30, 2017, 05:01:44 PM by Lar »

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4230
Re: Issue with the Round() function
« Reply #4 on: December 31, 2017, 03:28:14 PM »
Also, don't forget we now have the Format statement -- this will let you format numbers in a variety of ways.

Lar is correct, MKS$ doesn't convert a floating point number to a *human-readable* string -- it just stores the raw bytes of the number in an 8-character string for compact storage in a data file. Depending on the number there may be unprintable characters in that string. CVS is used to convert that string back into the original number.
« Last Edit: December 31, 2017, 03:31:40 PM by DrollTroll »
25 years with DesignCAD

jwillard

  • Newbie
  • *
  • Posts: 12
Re: Issue with the Round() function
« Reply #5 on: December 31, 2017, 04:02:51 PM »
Dr Pr... It's 32 bit Windows and I'm using DesignCAD 3D MAX 2018.

Lar... I headed down the "Precision" road but.... I tried to change it in the drawing I applied the Macro too, NOT in the macro itself. I'll give that a shot to deal with the trailing zero's.
As for the converting numbers to strings, what I'm trying to accomplish is use the For Next counter to set the text property in a Text Entity. In other words, I want to number the rows and columns of lines I'm drawing. In my testing when I try to convert a number to a string I get gibberish, as shown in my other post but... when I convert the string back to a number, it's correct.

Droll Troll... The documentation must be old then. The example in the doc's is what I tried, and why I was confused.  I just looked at the Format statement and it looks like it will do what I need.

Thanks all and Happy New Year
Jeff