I'm working to implement a custom string format. These are the float values to convert into a string:


This is the output I'm getting:


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)
    waveDurationFormat = waveDuration.ToString(".#######", numericProvider)
End If

  • \$\begingroup\$ Not a code review, but of the algorithm you chose: wouldn't it make more sense to use scientific notation when there are many leading zeros, rather than many significant fractional digits? Or more generally, when the number of total digits is 2 or 3 greater than the number of significant digits, so you'd also scientific for 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 and 0.01234567 would both stay as-is, so they visually line up for humans. Not so for large numbers though.) \$\endgroup\$ Commented Mar 16, 2022 at 18:55
  • \$\begingroup\$ Hello, yes, you're right. But the tool, I'm working on, is for a custom-made engine for a video game, and for some unknown reason uses this format. But yeah, it would make more sense of doing it with this other way. \$\endgroup\$
    – jms2505
    Commented Mar 18, 2022 at 23:32
  • \$\begingroup\$ Does it not accept numbers like 1.2E5f, but does accept 1.2345E5f? Or it prints numbers that way and you're trying to exactly match its output? \$\endgroup\$ Commented Mar 18, 2022 at 23:52
  • \$\begingroup\$ Well, with this code I've got the same output, the original exporter also prints the numbers in this way. \$\endgroup\$
    – jms2505
    Commented Mar 22, 2022 at 9:16
  • \$\begingroup\$ Ok, so you are just trying to match its exact output for some reason. (Perhaps so text diffs work better). Not because something else would reject this input if you made saner choices. \$\endgroup\$ Commented Mar 22, 2022 at 17:30

1 Answer 1


Welcome to Code Review! Nice first question.

Instead of using Instr() to check if stringNum contains a . you can just use String.IndexOf() and check if the result is > -1 which means the string representation of waveDuration contains a ..

If IndexOf() returns a value greater than -1 we can just check if stringNum.Length - stringNum.IndexOf(".") > 8. This has the advantage that the code won't create a new string each time it is called because we don't need to call Substring() anymore.

I would place this whole stuff inside an Extension method having an optional parameter stating the numbers of decimalplaces like so

Imports System.Globalization
Imports System.Runtime.CompilerServices
Module StringExtensions
    Dim numericProvider As New NumberFormatInfo With {.NumberDecimalSeparator = "."}

    Public Function ToCustomString(ByVal value As Single, Optional scientificNotationDecimalPlaces As Integer = 8) As String
        If scientificNotationDecimalPlaces < 0 Then
            Throw New ArgumentOutOfRangeException(NameOf(scientificNotationDecimalPlaces))
        End If

        Dim stringValue As String = value.ToString(numericProvider)

        Dim decimalPointIndex As Integer = stringValue.IndexOf(".")

        If decimalPointIndex > -1 AndAlso stringValue.Length - decimalPointIndex > scientificNotationDecimalPlaces Then
            Return value.ToString("0.######E+00", numericProvider)
        End If

        Return value.ToString(".#######", numericProvider)

    End Function
End Module  

You could then call it like so


or e.g if you want to have the sientific notation only if the decimal places are greater than 5 like so


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.