我该如何格式化十进制数? [英] How can I format decimal number ?

查看:108
本文介绍了我该如何格式化十进制数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

号码是这样的:123456789.987654321



如何制作这种格式(仅限数字的主要部分)123,456,789.987654321



我的意思是不要触摸十进制数字XXX,XXX,XXX.987654321



我每隔3位数就输入逗号我发现这段代码但不能工作所有number.length和十进制数



我找到了这些代码,但是当数字长度较长并且包含十进制数字时不能正常工作。



我的尝试:



the number is this : 123456789.987654321

How can make this format (only main part of number) 123,456,789.987654321

I mean to dont touch decimal numbers XXX,XXX,XXX.987654321

I tought to put comma every 3 digits I found this code but not work in all number.length and also with decimal number

I found this Codes but not work when number lenght is longer and if Decimal numbers inqluded.

What I have tried:

<pre>Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim Number As String = TextBoxOprtr1.Text
        If Number.ToString.Length = 4 Then
            Dim FirstDigit As String = Mid(Number, 1, 1)
            Dim RemainingDigits As String = Mid(Number, 2, 3)
            TextBoxOprtr2.Text = FirstDigit & "," & RemainingDigits 'Data update statement goes here'
        End If
        If Number.ToString.Length = 5 Then
            Dim FirstDigits As String = Mid(Number, 1, 2)
            Dim RemainingDigits As String = Mid(Number, 3, 3)
            TextBoxOprtr2.Text = FirstDigits & "," & RemainingDigits
        End If

        If Number.ToString.Length = 6 Then
            Dim FirstDigits As String = Mid(Number, 1, 3)
            Dim RemainingDigits As String = Mid(Number, 4, 3)
            TextBoxOprtr2.Text = FirstDigits & "," & RemainingDigits
        End If
        If Number.ToString.Length = 7 Then
            Dim FirstDigits As String = Mid(Number, 1, 1)
            Dim NextDigits As String = Mid(Number, 2, 3)
            Dim RemainingDigits As String = Mid(Number, 5, 3)
            TextBoxOprtr2.Text = FirstDigits & "," & NextDigits & "," & RemainingDigits
        End If

推荐答案

从文本框中读取字符串,并使用TryParse将其转换为数字。如果它赢了; t转换,它不是数字,所以告诉用户不要再做了。

然后使用string.Format将你的值转换成逗号分隔的字符串:

Read the string from the text box, and use TryParse to convert it to a number. If it won;t convert, it's not a number, so tell the user and do no more.
Then use string.Format to convert your value to a comma separated string:
Dim result As String = String.Format("{0:#,##0.##}", x)


这篇关于我该如何格式化十进制数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆