Author Topic: add leading zeros to DS2 value  (Read 1093 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
add leading zeros to DS2 value
« on: June 07, 2018, 04:17:08 PM »
How would I go about adding leading zeros to the value of a data subscriber2?  For example I want to display text in a textbox as " 00012" but the DS2 returns just "12" for its value.  I do not want to use concat.  Is there a format specifier method or something?  Thank you. 

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: add leading zeros to DS2 value
« Reply #1 on: June 07, 2018, 04:32:20 PM »
try : Value.ToString("D5")
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: add leading zeros to DS2 value
« Reply #2 on: June 07, 2018, 04:51:38 PM »
I should add I am extracting the elements of the DS2.  My code looks like this and I want to have a leading zero anytime there is a single digit. Thank you.   

blblControllerTime.Text = e.Values(0) & ":" & e.Values(1) & ":" & e.Values(2)  'HH:MM:SS

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: add leading zeros to DS2 value
« Reply #3 on: June 07, 2018, 05:30:44 PM »
Just off the top of my head, I think it is this:

e.Values(0).PadLeft(4,"0"c)

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: add leading zeros to DS2 value
« Reply #4 on: June 08, 2018, 08:00:25 AM »
Regular expression is also cool:
Code: [Select]
Dim someText As String = "Process Value 321 is Too High"
        someText = Regex.Replace(someText, "\d+", Function(n) n.Value.PadLeft(8, "0"c))
        ' ouput of someText = "Process Value 00000321 is Too High"
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================