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

前端 未结 3 1012
萌比男神i
萌比男神i 2021-01-11 19:56

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 co

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-11 20:14

    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.

提交回复
热议问题