Page 1 of 1

Formatting a numeric as a string - Issue?

Posted: Mon Apr 06, 2015 9:49 pm
by Zryip TheSlug
If I'm following the "format" documentation about casting a numeric as a string

Code: Select all

public handler myHandler()
   variable tNum as Number
   variable tNumString as String

   put 5 into tNum
   format tNum as string
   put the result into tNumString
   log tNumString
end handler
The result is not 5 but 5.000000 even if the variable tNum is defined as an Integer.

if we have this handler

Code: Select all

public handler myHandler()
   variable tNum as Number

   put 5 into tNum
   log tNum
end handler
The result is 5

If we are introducing a loop:

Code: Select all

public handler myHandler()
  variable tLine as Integer
  repeat with tLine from 1 up to 3
      log tLine
  end repeat
end handler
The result is:
1
2.000000
3.000000

Re: Formatting a numeric as a string - Issue?

Posted: Mon Apr 06, 2015 10:02 pm
by trevordevore
This is a known issue. For the time being, @LCMark gave me his handler to use when formatting a number as a string and you just want the integer representation.

Code: Select all

handler FormatInt(in pNumber as Number) returns String
	variable tNumberString as String

	put pNumber formatted as string into tNumberString

	if "." is in tNumberString then
		variable tDotOffset
		put the first offset of "." in tNumberString into tDotOffset
		delete char tDotOffset to (the number of chars in tNumberString) of tNumberString
	end if

	return tNumberString
end handler

Re: Formatting a numeric as a string - Issue?

Posted: Tue Apr 07, 2015 7:44 pm
by Zryip TheSlug
Thanks for sharing, Trevor.

Re: Formatting a numeric as a string - Issue?

Posted: Tue Apr 07, 2015 9:31 pm
by peter-b
trevordevore wrote:This is a known issue. For the time being, @LCMark gave me his handler to use when formatting a number as a string and you just want the integer representation.
That was me, but I'm flattered to be confused with @LCMark.

Unfortunately this has turned out to be an (even) more challenging fix that we expected, so I'm afraid it won't be fixed in 8.0.0-dp-2. Sorry. :cry:

Re: Formatting a numeric as a string - Issue?

Posted: Tue Apr 07, 2015 10:20 pm
by trevordevore
@peter-b - AKA @LCMark