VB.net Converting integer to desired format - vb.net

Code
myObject.TotalTime = 745
Dim test As Integer = Integer.Parse(myObject.TotalTime)
Dim test2 As String = test.ToString("00:00:00")
Desired result : 7:45:00
Current result : "00:07:45"
anyone knows a more sufficient way to convert this? like 2lines of code or even 1 line with a use of casting and such if only possible?

You can use DateTime.ParseExact() method and use h:mm:ss format string like this:
myObject.TotalTime = 745
Dim test As Integer = Integer.Parse(myObject.TotalTime)
' since it may contains 4 digits (e.g. 1000),
' use 4 digits for parsing instead of 3
Dim test1 As DateTime = DateTime.ParseExact(test.ToString("0000"), "HHmm", CultureInfo.InvariantCulture)
Dim test2 As String = test1.ToString("h:mm:ss")
Result: 7:45:00
Working example: .NET Fiddle Demo
Related issue: Convert numbers to time?

Related

Kotlin how to make 123.456 format

I have scales application. I want to display i.e. 123.456g
I have uint8 that represents 123 (scales range is 0 - 150g)
I also have uint16 that represents 456 (scales range .0 - .999g)
My code:
val strValue = characteristic.getIntValue(FORMAT_UINT16,0).toFloat() + ((characteristic.getIntValue(FORMAT_UINT16,1) or characteristic.getIntValue(FORMAT_UINT16,2).shl(8)) / 1000f)
Produces 51323.457 instead of 123.456
What is wrong?
You're adding floats, not generating a string.
Convert your digits to String, then concatenate them.

Convert hex to binary while retaining their places

I have this code that converts 7 to binary.
Dim s As String = "7"
Dim i As Integer = Convert.ToInt32(s, 16)
Dim s2 As String = Convert.ToString(i, 2)
Console.WriteLine(s2) 'result is 111
My problem is this, I want to apply the 8 bit binary so I can easily substring the result.
Instead of the result that is 111, I would like to make it 0111.
Examples:
Binary / Hex
1. F = 1111
2. 7 = 0111
3. 1 = 0001
You can pad the result with starting zero by using PadLeft.
s2.PadLeft(4, "0")

How to convert string to byte in Visual Basic

I'm trying to simulate an algoritham in cryptography and I need to convert a string of 0s and 1s back into a word. Example:
I have: 01011110010101101000001101100001101
I have split it into an array of strings:
0101111, 0010101, ...
each member has 7 characters. I want to get a letter that 0101111 represents in UTF8? How do I do this?
I try CType("0010101", Byte), but it fails. I can pass max 111 this way.
Help :/
UTF-8 is 8 bit, those are only 7 bits. Do you mean 7 bit ASCII?
In that case here you go:
Function BinToStr(binStr As String) As String
Dim i As Long
For i = 0 To (Len(binStr) / 7) - 1
[A1] = CLng(Mid(binStr, i * 7 + 1, 7))
BinToStr = BinToStr & Chr([BIN2DEC(A1)])
Next
End Function
If that's not what you're looking for, let me know.

Format a number to display a comma when larger than a thousand

I am writing some code in Visual Basic.net and have a question.
If I have a long number, that is larger than 1000, how can I format this value to be 1,000 (with a comma) and for this to be stored in a string?
For e.g.
1234 will be stored as 1,234
12345 will be stored as 12,345
123456 will be stored as 123,456
Is this done with a TryParse statement?
May I have some help to so this?
Take a look at The Numeric ("N") Format Specifier
General use:
Dim dblValue As Double = -12445.6789
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
' Displays -12,445.68
If you are only using integers then the following:
Dim numberString As String = 1234.ToString("N0")
Will show numberString = "1,234" as the "N0" format will not add any figures after a decimal point.
For those wanting to do a currency with commas and decimals use the following: .ToString("$0,00.00")
Using $ notation:
int myvar = 12345;
Console.WriteLine($"Here is my number: {myvar:N0}");

Format number to string

I have to format number to exact this format "###,###,###.##" and try to write program like this:
Dim myNum as double = 1255.32
Debug.Print(myNum.ToString("###,###,###.##"))
I can do .PadLeft to ensure aligning with fixed-width font but my number is not showed correctly with this format string.
If I write that by using "##0.00" then I haven't thousand separator showed.
In earlier Basic versions that was easy but...
How to get number showed in this format in VB.NET?
If is important my local decimal "point" is "," (comma).
Example:
First source Second source
---------------- ----------------
Price: 97.419,52 97.419,26
Tax: 4.870,98 4.870,96
Brutto: 102.290,50 102.290,24
Temp source
----------------
Price: 0,00
Tax: 0,00 Difference
Brutto: 0,00 - 0,26
CultureInfo gives you much more control on the number format. Sample code:
Dim culture As Globalization.CultureInfo = New Globalization.CultureInfo(Globalization.CultureInfo.CurrentCulture.Name)
culture.NumberFormat.NumberDecimalSeparator = "."
culture.NumberFormat.NumberGroupSeparator = ","
Dim myNum As Double = 1255.32
Debug.Print(myNum.ToString("N", culture))