How to display a float to 2 decimal places in VB.NET

4 Answers

0 votes
Imports System.IO

Module Module1

    Sub Main()

        Dim f As Double = 0.314F

        Try

            Console.WriteLine(f.ToString("0.00"))

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

    End Sub

    'run:
    ' 
    '0.31

End Module

 



answered Jun 4, 2015 by avibootz
0 votes
Imports System.IO

Module Module1

    Sub Main()

        Dim f As Double = 0.3554F

        Try

            Console.WriteLine(f.ToString("0.00"))

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

    End Sub

    'run:
    ' 
    '0.36

End Module

 



answered Jun 4, 2015 by avibootz
0 votes
Imports System.IO

Module Module1

    Sub Main()

        Dim f As Double = 0.314F

        Try

            Console.WriteLine(f.ToString("n2"))

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

    End Sub

    'run:
    ' 
    '0.31

End Module

 



answered Jun 4, 2015 by avibootz
0 votes
Imports System.IO

Module Module1

    Sub Main()

        Dim f As Double = 0.314F

        Try

            Console.WriteLine(String.Format("{0:0.00}", f))

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

    End Sub

    'run:
    ' 
    '0.31

End Module

 



answered Jun 4, 2015 by avibootz

Related questions

1 answer 139 views
1 answer 162 views
4 answers 399 views
2 answers 364 views
4 answers 537 views
2 answers 394 views
4 answers 184 views
...