|
-
Apr 10th, 2023, 04:27 AM
#1
Thread Starter
Hyperactive Member
[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)
-
Apr 10th, 2023, 04:39 AM
#2
Thread Starter
Hyperactive Member
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.
-
Apr 10th, 2023, 07:49 AM
#3
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
-
Apr 10th, 2023, 08:48 AM
#4
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).
-
Apr 10th, 2023, 09:02 AM
#5
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
-
Apr 12th, 2023, 11:43 PM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|