Assistance with Formatting and Parsing Milliseconds in Visual Studio

Anda Mbambo 0 Reputation points
2024-08-18T21:37:06.6533333+00:00

I’m experiencing issues with a string called "Milliseconds" in my application. When the "NumberFormatter returns its correct value it doesn't want to use this value and keeps the string it already has. The strings Seconds, Minutes and Hours all accept the the returned value from the NumberFormatter method but for some reason this isn't the case with Milliseconds. They all use the same method to format the strings yet Milliseconds doesn't want to assign a new value. Is there something about Visual Studio that I'm not understanding that may be causing this because I don't think it's a bug with the IDE. (also, I checked and the NumberFormatter actually returns Milliseconds's correct value however when it is returned Milliseconds doesn't change to the correct value)

Also, let's say for instance Milliseconds is "1" instead of the "001" it's supposed to be which is the case at the moment since I haven't solved this issue. Even though this is incorrect, why does DateTime.Parse format the "1" as "100" and not "001" or just "1". This is even more confusing.

I'm using VS Version 17.10.5.

Milliseconds = NumberFormatter(this.spinner_MILLISECONDS.Digits - Milliseconds.Length, Milliseconds);

Seconds = NumberFormatter(this.spinner_SECONDS.Digits - Seconds.Length, Seconds);

Minutes = NumberFormatter(this.spinner_MINUTES.Digits - Minutes.Length, Minutes);

Hours = NumberFormatter(this.spinner_HOURS.Digits - Hours.Length, Hours);

this.Time = DateTime.Parse(
Hours + ":" +
Minutes + ":" +
Seconds + "." +
Milliseconds);

public string NumberFormatter(int realLength, string currentNumber)
{
    if (realLength != 0 && realLength > 0)
    {
        const string zero = "0";
        for (int i = 0; i < realLength; i++)
            currentNumber = zero + currentNumber;
    }
    return currentNumber;
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,763 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,874 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 54,221 Reputation points
    2024-08-20T21:11:17.97+00:00

    I think your number formatter is overly complex. If I understand correctly, don't you just want to ensure that milliseconds are 3 digits and seconds/minutes/hours are 2 digits? If so and you are certain the string value is a number then just let the framework format it for you.

    string NumberFormatter ( int width, string value )
    {
        if (Int32.TryParse(value, out var number))
            return number.ToString(new string('0', width));
    
        return "";
    }
    
    //Calling
    milliseconds = NumberFormatter(3, milliseconds);
    

    Alternatively you could go with padding instead.

    string NumberFormatter ( int width, string value )
    {
       return value.PadLeft(width, '0');
    };
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.