none
Formating numbers RRS feed

  • Question

  •  Hi 
    I want to format a number so that trailing zero is only displayed if directly after the decimal point as in

    Dim diameter as double

    diameter = 1 would output 1.0
    diameter = 1.1 would output 1.1
    diameter =  1.12 would output 1.12
    diameter = 1.123 would output 1.123

    In other words if the number is an integer I want to display the decimal point and a trailing zero,
    for any other decimal, no trailing zeros.

    I've been using format number:

    FormatNumber(diameter, 3) but that always gives three decimal places even if they are all zero (1.000)

    Do I need to build a special function for this?

    Cheers

    Andy

    Wednesday, November 29, 2017 1:58 PM

Answers

  • Hi

    Maybe something like this:

            ' Produces 1.0
            Dim num As Double = 1
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.1
            num = 1.1
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.12
            num = 1.12
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.123
            num = 1.123
            Label1.Text = num.ToString("#.0##")
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by dbasnett Wednesday, November 29, 2017 2:18 PM
    • Marked as answer by AndyNakamura Wednesday, November 29, 2017 3:31 PM
    Wednesday, November 29, 2017 2:15 PM

All replies

  • Hi

    Maybe something like this:

            ' Produces 1.0
            Dim num As Double = 1
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.1
            num = 1.1
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.12
            num = 1.12
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.123
            num = 1.123
            Label1.Text = num.ToString("#.0##")
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by dbasnett Wednesday, November 29, 2017 2:18 PM
    • Marked as answer by AndyNakamura Wednesday, November 29, 2017 3:31 PM
    Wednesday, November 29, 2017 2:15 PM
  •  Hi 
    I want to format a number so that trailing zero is only displayed if directly after the decimal point as in

    Dim diameter as double

    diameter = 1 would output 1.0
    diameter = 1.1 would output 1.1
    diameter =  1.12 would output 1.12
    diameter = 1.123 would output 1.123

    In other words if the number is an integer I want to display the decimal point and a trailing zero,
    for any other decimal, no trailing zeros.

    I've been using format number:

    FormatNumber(diameter, 3) but that always gives three decimal places even if they are all zero (1.000)

    Do I need to build a special function for this?

    Cheers

    Andy

    Try this and see if it does what you want:

        Private Function _
            GetFormattedString(ByVal value As Double) As String
    
            Dim retVal As String = Nothing
    
            Dim length As Double = value - Fix(value)
    
            If length = 0 Then
                retVal = value.ToString("n1")
            Else
                retVal = value.ToString
            End If
    
            Return retVal
    
        End Function


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, November 29, 2017 2:26 PM
  • Hi

    Maybe something like this:

            ' Produces 1.0
            Dim num As Double = 1
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.1
            num = 1.1
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.12
            num = 1.12
            Label1.Text = num.ToString("#.0##")
    
            ' Produces 1.123
            num = 1.123
            Label1.Text = num.ToString("#.0##")


    Regards Les, Livingston, Scotland


    Good answer.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Wednesday, November 29, 2017 3:21 PM
  • Thanks Les, that works great. I forgot to mention that I need the result to 3 decimal places and your solution does just that.

    Just need to research how it works now!

    Wednesday, November 29, 2017 3:34 PM
  • Thanks Les, that works great. I forgot to mention that I need the result to 3 decimal places and your solution does just that.

    Just need to research how it works now!

    Take a look here

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Wednesday, November 29, 2017 3:38 PM