Results 1 to 6 of 6

Thread: [RESOLVED] String format like a pro

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] String format like a pro

    Consider doing following items at a same time:
    - Calculate a value.
    - Fix decimal numbers maximum at two places (or maybe round).
    - Always use sign in prefix (In another meaning always "+", in case of negative numbers switch to "-").
    - Which unit is it? Add it as a suffix.

    I only know this way so far:
    Code:
    MyLabel.Text = String.Format("+0.00", MySingleVariable / 1000) 'is ToString needed?
    'If/Else reguired for desission making whether UnitA or UnitB is required.
    I'm sure Visual Studio developers are already dedicated a shortened, magical, one-lined, command for it cause they've encountered and struggled with this difficulty before.

    (Not familiar with below structure: I'm not sure is it helpful or not)
    Code:
    MyLabel.Text = MySingleVariable.ToString(Format As String, Provider As IFormatProvider)

  2. #2

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: String format like a pro

    Oh quick update:

    There were a similar thing in microcontrollers once upon a time which you could easily switch between d (decimals), s (strings), f (floats), x (hexadecimals in lower cases), X (hexadecimals in upper cases).

    Is there a similar thing here? Regarding to the fact VS has a variety of Data Types and Variable Genders (Like Shorts, Integers, Singles, Doubles and even Date which you should read about them first)

    ---
    I'm also looking for contractual/reserved terms and symbols between them like "ddd\:hh\:mm\:ss". Therefore, external links are welcome here. <3
    Code:
    Dim elapseTime As Single = performancecounterTIME.NextValue
    Dim d = TimeSpan.FromSeconds(CDbl(elapseTime.ToString))
    Uptime.Text = d.ToString"ddd\:hh\:mm\:ss"
    Last edited by pourkascheff; Apr 10th, 2023 at 07:12 AM.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: String format like a pro

    First this is invalid syntax =
    Code:
    Uptime.Text = d.ToString"ddd\:hh\:mm\:ss"
    It should be:
    Code:
    Uptime.Text = d.ToString("ddd\:hh\:mm\:ss")
    ToString is a function, so you must pass the format parameter inside of parenthesis...

    That said...
    String Interpolation is where it's at these days ....

    Code:
    Dim elapseTime As Single = performancecounterTIME.NextValue
    Dim d = TimeSpan.FromSeconds(CDbl(elapseTime.ToString))
    Uptime.Text = $"{d:ddd:hh:mm:ss}"
    More resources: https://www.tutlane.com/tutorial/vis...-interpolation
    Search results https://search.brave.com/search?q=vb...tes&source=web

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: String format like a pro

    Code:
    MyLabel.Text = String.Format("+0.00", MySingleVariable / 1000) 'is ToString needed?
    No, per the documentation:
    Converts the value of objects to strings based on the formats specified and inserts them into another string.
    And to techgnome's point, this is also in the same documentation:
    Instead of calling the String.Format method or using composite format strings, you can use interpolated strings if your language supports them. An interpolated string is a string that contains interpolated expressions. Each interpolated expression is resolved with the expression's value and included in the result string when the string is assigned. For more information, see String interpolation (C# Reference) and Interpolated Strings (Visual Basic Reference).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: String format like a pro

    To take it one step further ... you can do your division in the interpolation:
    Code:
    $"{MySingleVariable / 1000:+f2}"
    Should work ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: String format like a pro

    Thanks all for contribution.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width