从字符串中获取金额并包含句点而不是逗号 [英] Get the Amount from a string and include period instead of comma

查看:166
本文介绍了从字符串中获取金额并包含句点而不是逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

有人可以帮忙吗?我有一个像货币价值的字符串。我只想获取不包括货币符号的实际金额。

例如我有两个金额 NZD12453,25 AUD2356

我想抓什么是 12453.25 2356

如果有逗号,则会放置逗号的插入逗号后面的值,如果逗号后面没有值,则只删除逗号和字母前面的字母,如 AUD NZD 并且只抓住数字。

Pease协助。我的下面的代码正在运行,但它正在给出删除逗号的金额。

 私有  Sub  Button1_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)< span class =code-keyword>句柄 Button1.Click 

Dim B32Stinrg 正如 字符串 = JPY12561694,60
Dim B32Stinrg2 作为 字符串 = JPY12532694,
昏暗 test2 作为 字符串 = JPY32561694,:77CDX

Dim TestDisplay1 As 字符串 = GetNumbersfromStringUsingSB(B32Stinrg.Replace( : 32B: ))
Dim TestDisplay2 作为 字符串 = GetNumbersfromStringUsingSB(test2.Replace( :32B: ))
Dim TestDisplay3 As String = GetNumbersfromStringUsingSB( B32Stinrg2.Replace( :32B: ))


MessageBox.Show( 此数值:32B:JPY12561694,60是& TestDisplay1, ,MessageBoxButtons.OK)
MessageBox.Show( 这个数值:32B:JPY12532694,是& TestDisplay3, ,MessageBoxButtons.OK)
MessageBox.Show( 这个数值:32B:JPY12561694,:77CDX是& TestDisplay2, ,MessageBoxButtons.OK)

结束 Sub

函数 GetNumbersfromStringUsingSB( ByVal thestring 作为 字符串作为 大号ong
Dim sb 作为 System.Text.StringBuilder(thestring.Length)
对于 每个 ch 作为 字符 thestring
如果 字符 .IsDigit(ch)那么
sb.Append(ch)
结束 如果
下一步
返回 .Parse( sb.ToString)
结束 功能

解决方案

您需要做的就是使用 Regex Class [ ^ ]模式如下: \d * \d 。您可以在此处找到更多模式: http://www.regular-expressions.info/reference.html [ ^ ]


的目的是什么?B32Stinrg.Replace(:32B:,)



随着留出来的,你的GetNumbersfromStringUsingSB方法只需要一些推文。首先,我改变它以返回Decimal而不是Int64;我假设你想要基于你描述的那个分数组件。其次,我添加条件来处理,或。的情况。



 功能 GetNumbersfromStringUsingSB( ByVal  thestring  As  字符串作为 十进制 
Dim sb As System.Text.StringBuilder(thestring.Length)
对于 每个 ch As 字符 thestring
如果 Char .IsDigit(ch)然后
sb.Append(ch)
ElseIf ch = c OrElse ch = c 然后
sb.Append ( c)
结束 如果
下一步
返回 十进制 .Parse(sb.ToString)
结束 功能


Hello,
Could someone please assist? I have a string which is like monetary value. I just want to grab the actual amount excluding the currency symbols.
E.g. I have two amounts as NZD12453,25 and AUD2356,
What I want to grab is 12453.25 and 2356
Insetad of the comma a period will be placed if there are values after the comma, if no values after the comma then just remove the comma and the letters infront like AUD and the NZD and grab only the numbers.
Pease assist. my code below is working but it''s giving the amount removing the comma.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim B32Stinrg As String = "JPY12561694,60"
       Dim B32Stinrg2 As String = "JPY12532694,"
       Dim test2 As String = "JPY32561694,:77CDX"

       Dim TestDisplay1 As String = GetNumbersfromStringUsingSB(B32Stinrg.Replace(":32B:", ""))
       Dim TestDisplay2 As String = GetNumbersfromStringUsingSB(test2.Replace(":32B:", ""))
       Dim TestDisplay3 As String = GetNumbersfromStringUsingSB(B32Stinrg2.Replace(":32B:", ""))


       MessageBox.Show("The numerical value for this :32B:JPY12561694,60 is " & TestDisplay1, "", MessageBoxButtons.OK)
       MessageBox.Show("The numerical value for this :32B:JPY12532694, is " & TestDisplay3, "", MessageBoxButtons.OK)
       MessageBox.Show("The numerical value for this :32B:JPY12561694,:77CDX is " & TestDisplay2, "", MessageBoxButtons.OK)

   End Sub

    Function GetNumbersfromStringUsingSB(ByVal thestring As String) As Long
       Dim sb As New System.Text.StringBuilder(thestring.Length)
       For Each ch As Char In thestring
           If Char.IsDigit(ch) Then
               sb.Append(ch)
           End If
       Next
       Return Long.Parse(sb.ToString)
   End Function

解决方案

All you need to do is to use Regex Class[^] with pattern like this: \d*\d. More patterns you''ll find here: http://www.regular-expressions.info/reference.html[^]


What is the purpose of B32Stinrg.Replace(":32B:", "")?

With that set aside, your "GetNumbersfromStringUsingSB" method just needs a few tweeks. First off I changed it to return a Decimal instead of an Int64; i assumed you want that fraction component based on what you described. Second, I added condition to handle then case of "," or ".".

Function GetNumbersfromStringUsingSB(ByVal thestring As String) As Decimal
    Dim sb As New System.Text.StringBuilder(thestring.Length)
    For Each ch As Char In thestring
        If Char.IsDigit(ch) Then
            sb.Append(ch)
        ElseIf ch = "."c OrElse ch = ","c Then
            sb.Append("."c)
        End If
    Next
    Return Decimal.Parse(sb.ToString)
End Function


这篇关于从字符串中获取金额并包含句点而不是逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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