I'm working to implement a custom string format. These are the float values to convert into a string:
1,02458
0,4145805
0,06231292
0,04362812
1,16458
1,172608
0,06011338
0,05324263
0,0653288
0,0285034
0,0992517
0,0861678
This is the output I'm getting:
1.02458f
.4145805f
6.231292E-02f
4.362812E-02f
1.16458f
1.172608f
6.011338E-02f
5.324263E-02f
.0653288f
.0285034f
.0992517f
.0861678f
The idea is, if the decimal places are greater than 8, use the scientific notation. If not, remove the left zeros. Now I'm using this code, but I would like to know if there's another more efficient way of doing this but always getting the same output:
Dim numericProvider As New NumberFormatInfo With {
.NumberDecimalSeparator = "."
}
'Get the number of decimal places
Dim stringNum As String = waveDuration.ToString(numericProvider)
Dim decimalPlaces As Integer = 0
If InStr(1, stringNum, ".", CompareMethod.Binary) Then
decimalPlaces = stringNum.Substring(stringNum.IndexOf(".")).Length
End If
'Format String
Dim waveDurationFormat As String
If decimalPlaces > 8 Then
waveDurationFormat = waveDuration.ToString("0.######E+00", numericProvider)
Else
waveDurationFormat = waveDuration.ToString(".#######", numericProvider)
End If
ListBox3.Items.Add(waveDurationFormat)
120000.0
=>1.2E5f
. This would avoid lengthening numbers, and also choose the same format for two small numbers of the same order of magnitude. (e.g.0.01
and0.01234567
would both stay as-is, so they visually line up for humans. Not so for large numbers though.) \$\endgroup\$1.2E5f
, but does accept1.2345E5f
? Or it prints numbers that way and you're trying to exactly match its output? \$\endgroup\$