vb.net 2010 中的格式化数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19220281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:22:52  来源:igfitidea点击:

formatting number in vb.net 2010

vb.net

提问by Umar

Im using this code for formatting my textbox.

我使用此代码格式化我的文本框。

Public Function TextFormat(ByVal sString As String) As String
        Dim num1 As Decimal
        Try
            num1 = Convert.ToDecimal(sString)
            TextFormat = FormatNumber(num1, 2)
            Return TextFormat
        Catch
            TextFormat = sString
        End Try
    End Function

-the problem is its rounding off the number i input .. sample

- 问题是它四舍五入我输入的数字..样本

textbox.text = "5999.99" it displaying "6000.00" how can i disable the auto rounding off number . or is there any other code for formatting text? "###,###,###.##" << it should be like this

textbox.text = "5999.99" 它显示 "6000.00" 如何禁用自动四舍五入数字。或者是否有其他用于格式化文本的代码?"###,###,###.##" << 应该是这样的

thanks

谢谢

回答by dovid

use ToString Instead FormatNumber.

使用 ToString 而不是 FormatNumber。

Public Function TextFormat(ByVal sString As String) As String
    Dim num1 As Decimal
    If Double.TryParse(sString, num1) Then
        Return num1.ToString("G") ' or ToString("F2") or ToString("0.00") 
    Else
        Return sString
    End If
End Function

回答by Yetispapa

Try to use:

尝试使用:

 Dim number = Math.Truncate(CInt(textbox.text) * 1000) / 1000;

 textbox.text = number.toString