-1

I'm trying to add 0 before each number I'm having but it seems it's not working because I'm using double. I'm using double so I can input numbers such as 1.5 (hours) and translate it to seconds, minutes, and hours (Output should be 0 seconds, 30 minutes, and 1 hour and 01:30:00). I'm having no problems with the first output but I can't seem to do the second output (The desired 01:30:00 only displays 1:30:0). What I did first is I tried to convert the double variable to int32 then to string but it seems to not work. Here's the code:

    If SecondsRemainder >= 0 And SecondsRemainder < 10 Then
            SecondsRemainder = Convert.ToInt32(SecondsRemainder)
            SecondsRemainder.ToString.PadLeft(2, "0")
    End If

This line of code:

    SecondsRemainder.ToString.PadLeft(2, "0")

Doesn't seem to do anything, am I missing something out? Or is there any other way I can do? Looking forward to your answers!

  • 1
    The same problem as in this question: https://stackoverflow.com/questions/18282903/vb-net-replace-not-working – MatSnow Oct 29 '20 at 13:39
  • 2
    Does this answer your question? [C# PadLeft not working](https://stackoverflow.com/questions/51139663/c-sharp-padleft-not-working) – GSerg Oct 29 '20 at 13:39
  • One thing to remember is **there is no such thing as in-place type conversion** in .Net. Once you declare a variable as a double, it _stays_ a double, and doubles are binary. They don't care about formatting issues like leading zeros. If you need a formatted string, you need to declare a **separate variable** for this. – Joel Coehoorn Oct 29 '20 at 14:04
  • @JoelCoehoorn I'll take note of that! Again, Thanks a lot! – Bryan Beltran Oct 29 '20 at 14:25
  • @BryanBeltran - if you are working with time values take a look at TimeSpan. See my answer below. – dbasnett Oct 29 '20 at 15:07

2 Answers2

1

Let's look at this line piece by piece, and see what it actually does:

SecondsRemainder.ToString.PadLeft(2, "0")

We start with the SecondsRemainder variable. This variable is still a Double, in spite of the earlier code using Convert.ToInt32(). Remember, at it's core VB.Net is a statically typed language! When you declare a variable with a specific type, the variable's type can never change.

We now call the .ToString() method for this variable. Note this really is a method, not a property. Good practice for .Net is to include the parentheses when calling methods, even though they aren't strictly required with VB. If I reviewed that code, I'd ask you to change it to show the parentheses. Remember, we're also getting the Double version of this method, rather than the Integer version. You're probably okay here, but the double version can do weird things for formatting you might not expect from an integer.

Finally, we take the string result from the previous method and call PadLeft(). This mostly does what you expect. However, there is no overload that takes a number and a string. Frankly, I'm surprised this even compiles, and it tells me you likely don't have Option Strict set correctly. No self-respecting programmer runs with Option Strict Off anymore. The correct way to call this function is like this:

.PadLeft(2, "0"c)

Where the c suffix gives you a character value rather than a string value.

And that's it. We're done. This function returns a result. It does not modify the calling variable. So we've done all this work, and discard the result without actually changing anything.

What I would do to fix your issue is declare a new string variable to receive the result. Then I would use this code to assign to it:

'Create a string variable to hold your string result
Dim RemainderString As String = ""
'Use double literals to compare with double variables!
If SecondsRemainder >= 0.0 And SecondsRemainder < 10.0 Then 
    'Use a format string directly from the initial double value to create your string result
    ' and don't forget to assign it to a variable
    RemainerString = SecondsRemainder.ToString("00") 
End If

You may also want to use Math.Round() first, as this code would still create "01" from a 1.9999 input.

Finally, I'm wondering how you're using this SecondsRemainder value. VB.Net has a whole set of methods for building date and time strings and values, and a variable name like SecondsRemainder sounds like you're doing something the hard way that could be much MUCH easier.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thank you for that well-explained information from top to bottom! All is clear to me and thank you for correcting me as well as providing the changes needed for my code. Really appreciated your help :D Godbless! – Bryan Beltran Oct 29 '20 at 14:11
0

I'm wondering if the answer is that you are approaching the problem incorrectly. You seem to be computing some time value. If so use TimeSpan.

    Dim ts As TimeSpan = TimeSpan.FromHours(1.51#)
    ' ts.TotalSeconds
    ' ts.Seconds
    Dim s As String = ts.ToString("hh\:mm\:ss")
dbasnett
  • 11,334
  • 2
  • 25
  • 33