News:

  • June 08, 2026, 05:42:07 AM

Login with username, password and session length

Author Topic: Can't parse an Integer with FmtInt in STRPRINT?  (Read 7586 times)

Bolt

  • Hero Member
  • *****
  • Posts: 598
Can't parse an Integer with FmtInt in STRPRINT?
« on: July 14, 2021, 10:30:28 AM »
Is there a way I can take the last 2 digits from a number, and display it in STRPRINT?  FmtInt(D0,dec,2) doesn't seem to do the trick, it will put all the digits in there.

I'm sure I can do it with STRPRINT, STRSUB, and then use STRPRINT again to use it in my longer string, but surely there's a way to do this all in one instruction?

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Re: Can't parse an Integer with FmtInt in STRPRINT?
« Reply #1 on: July 14, 2021, 10:47:13 AM »
You might want to use MOD 100 first to isolate the last two digits.
An output is a PLC's way of getting its inputs to change.

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Can't parse an Integer with FmtInt in STRPRINT?
« Reply #2 on: July 14, 2021, 10:59:01 AM »
Thanks Bernie, I got so focused on strings in forgot about simple math...

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: Can't parse an Integer with FmtInt in STRPRINT?
« Reply #3 on: July 14, 2021, 11:13:56 AM »
If you want 102 to display as 02, don't forget to set the Justification parameter in FmtInt to ZeroPad.

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Can't parse an Integer with FmtInt in STRPRINT?
« Reply #4 on: July 14, 2021, 11:27:55 AM »
If you want 102 to display as 02, don't forget to set the Justification parameter in FmtInt to ZeroPad.
Yes, I made sure to do that.

Still, shouldn't STRPRINT FmtInt(D0,dec,2,zeropad) take a longer number and only print the 2 digits, without first having to do MATH D0 = D1 % 100?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: Can't parse an Integer with FmtInt in STRPRINT?
« Reply #5 on: July 14, 2021, 11:50:08 AM »
Still, shouldn't STRPRINT FmtInt(D0,dec,2,zeropad) take a longer number and only print the 2 digits, without first having to do MATH D0 = D1 % 100?

No.  That is the minimal width, not maximum width.  That's how most numeric- text formatting mechanisms work.  If you want to truncate it, do you want the first 2 digits or the last 2 digits?
12345 (12 or 45?)