如何通过在另一个文本框中提供数值来在文本框中显示单词中的数字? [英] How to display numbers in words in textbox by providing a numeric value in another text box?

查看:54
本文介绍了如何通过在另一个文本框中提供数值来在文本框中显示单词中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有一个文本框,命名为Txt_Budget,它将数字作为输入。我想将此数值转换为单词并在另一个文本框上显示该值,即只使用Txt_Budget_In_Word_click事件。

注意我不能使用按钮。



Thnx

解决方案

Public Class Form1

Dim WordStr As String

公共函数NoToWord(ByVal Acceptno As Double)As String

NoToWord =



Dim FormatStr As String

FormatStr =格式(Acceptno,000000000.00)



如果是Val(mid( FormatStr,1,2))<> 0然后

调用Word(Val(mid(FormatStr,1,2)))

NoToWord = StrConv(NoToWord&Trim(WordStr)&CRORE,vbProperCase)

结束如果

如果Val(mid(FormatStr,3,2))<> 0然后

调用Word(Val(mid(FormatStr,3,2)))

NoToWord = StrConv(NoToWord&Trim(WordStr)&LAC,vbProperCase)

结束如果

如果Val(mid(FormatStr,5,2))<> 0然后

调用Word(Val(mid(FormatStr,5,2)))

NoToWord = StrConv(NoToWord&Trim(WordStr)&THOUSAND,vbProperCase)

结束如果

如果Val(mid(FormatStr,7,1))<> 0然后

调用Word(Val(mid(FormatStr,7,1)))

NoToWord = StrConv(NoToWord&Trim(WordStr)&HUNDRED,vbProperCase)

结束如果

如果Val(mid(FormatStr,8,2))<> 0然后

调用Word(Val(mid(FormatStr,8,2)))

NoToWord = StrConv(NoToWord&Trim(WordStr)&,vbProperCase)

结束如果

如果Val(mid(FormatStr,11,2))<> 0然后

调用Word(Val(mid(FormatStr,11,2)))

If(NoToWord<>)然后

NoToWord = StrConv(Rs。&NoToWord&AND&Trim(WordStr)&PAISE ONLY,vbProperCase)

Else

NoToWord = StrConv( NoToWord&Trim(WordStr)&PAISE ONLY,vbProperCase)

结束如果

否则

如果NoToWord<> 然后

NoToWord = StrConv(Rs。&NoToWord +ONLY,vbProperCase)

Else

NoToWord =(In单词)

结束如果

结束如果

结束功能



Sub Word(ByVal num As Byte)

Dim X(9)As String

Dim x1(9)As String

Dim x2(9)作为字符串





X(1)=ONE:X(2)=TWO:X(3)=三:X(4)=四:X(5)=五:X(6)=六:X(7)=七:X(8)=八:X( 9)=NINE

x1(1)=TEN:x1(2)=TWENTY:x1(3)=THIRTY:x1(4)=FORTY: x1(5)=FIFTY:x1(6)=SIXTY:x1(7)=SEVENTY:x1(8)=EIGHTY:x1(9)=NINETY

x2(1)=ELEVEN:x2(2)=TWELVE:x2(3)=THIRTEEN:x2(4)=FOURTEEN:x2(5)=FIFTEEN:x2(6 )=十六:x2(7)=SEVENTEEN:x2(8)=八EEN:x2(9)=NINETEEN





如果num< = 9那么'为1到9
WordStr = X(num)

结束如果

如果num> = 10那么'为10到99

如果(num Mod 10 = 0)然后'为10,20,30 .... 90

WordStr = x1(Int(num / 10))

结束如果

If(Int(num / 10)= 1 And(num Mod 10<> 0))然后'为11,12,13 ..... 19

WordStr = x2(num Mod 10)

结束如果

如果(Int(num / 10)> 1和(num Mod 10<> 0))则'for 21,22,23 ..... 99

WordStr = x1( Int(num / 10))+ X(num Mod 10)

结束如果

结束如果

结束子



Private Sub TextBox1_LostFocus(ByVal sender As Object,ByVal e As System.EventArgs)处理TextBox1.LostFocus

TextBox2.Text = NoToWord(TextBox1.Text)

结束子



结束类


这是一个更短的代码。一个RegEx,没有循环。根据需要转换,在南亚编号系统中

 



  var  a = [' ''  one'' 两个'' 三个 ' 四'' 五''  six'' 七'' 八''  nine''  ten''  11'','  12'' 十三'' 十四'' 十五''  16 十七'' 十八' ' 十九']; 
var b = [' '' '' < span class =code-string> 20',' 30'' 四十'' 50'' sixty'' 七十'' 八十'' ninety'];

函数inWords(num){
if ((num = num.toString())。length > 9 return ' overflow';
n =(' 000000000' + num).substr(-9).match(/ ^(\d { 2 })(\d { 2 })(\d { 2 })(\d { 1 })(\d { 2 })<​​/ BLOCKQUOTE>
/);
if (!n) return ; var str = ' ';
str + =(n [ 1 ]!= 0 )? (a [数字(n [ 1 ])] || b [n [ 1 ] [ 0 ]] + ' ' + a [n [ 1 ] [ 1 ]])+ ' crore'' ';
str + =(n [ 2 ]!= 0 )? (a [数字(n [ 2 ])] || b [n [ 2 ] [ 0 ]] + ' ' + a [n [ 2 ] [ 1 ]])+ ' lakh'' ';
str + =(n [ 3 ]!= 0 )? (a [数字(n [ 3 ])] || b [n [ 3 ] [ 0 ]] + ' ' + a [n [ 3 ] [ 1 ]])+ ' 千'' ';
str + =(n [ 4 ]!= 0 )? (a [数字(n [ 4 ])] || b [n [ 4 ] [ 0 ]] + ' ' + a [n [ 4 ] [ 1 ]])+ ' 百'' ';
str + =(n [ 5 ]!= 0 )? ((str!= ' ')?' 和'' ') +(a [数字(n [ 5 ])] || b [n [ 5 ] [< span class =code-digit> 0 ]] + ' ' + a [ n [ 5 ] [ 1 ]])+ ' 仅'' ' ;
return str;
}


Hi,
i have a textbox naming Txt_Budget which takes numbers as input.i want to covert this numeric value into words and display the value on another textbox i.e just by using Txt_Budget_In_Word_click event.
Note i cannot use Buttons.

Thnx

解决方案

Public Class Form1
Dim WordStr As String
Public Function NoToWord(ByVal Acceptno As Double) As String
NoToWord = ""

Dim FormatStr As String
FormatStr = Format(Acceptno, "000000000.00")

If Val(mid(FormatStr, 1, 2)) <> 0 Then
Call Word(Val(mid(FormatStr, 1, 2)))
NoToWord = StrConv(NoToWord & Trim(WordStr) & " CRORE ", vbProperCase)
End If
If Val(mid(FormatStr, 3, 2)) <> 0 Then
Call Word(Val(mid(FormatStr, 3, 2)))
NoToWord = StrConv(NoToWord & Trim(WordStr) & " LAC ", vbProperCase)
End If
If Val(mid(FormatStr, 5, 2)) <> 0 Then
Call Word(Val(mid(FormatStr, 5, 2)))
NoToWord = StrConv(NoToWord & Trim(WordStr) & " THOUSAND ", vbProperCase)
End If
If Val(mid(FormatStr, 7, 1)) <> 0 Then
Call Word(Val(mid(FormatStr, 7, 1)))
NoToWord = StrConv(NoToWord & Trim(WordStr) & " HUNDRED ", vbProperCase)
End If
If Val(mid(FormatStr, 8, 2)) <> 0 Then
Call Word(Val(mid(FormatStr, 8, 2)))
NoToWord = StrConv(NoToWord & Trim(WordStr) & " ", vbProperCase)
End If
If Val(mid(FormatStr, 11, 2)) <> 0 Then
Call Word(Val(mid(FormatStr, 11, 2)))
If (NoToWord <> "") Then
NoToWord = StrConv("Rs. " & NoToWord & "AND " & Trim(WordStr) & " PAISE ONLY", vbProperCase)
Else
NoToWord = StrConv(NoToWord & Trim(WordStr) & " PAISE ONLY", vbProperCase)
End If
Else
If NoToWord <> "" Then
NoToWord = StrConv("Rs. " & NoToWord + " ONLY", vbProperCase)
Else
NoToWord = "(In Words )"
End If
End If
End Function

Sub Word(ByVal num As Byte)
Dim X(9) As String
Dim x1(9) As String
Dim x2(9) As String


X(1) = "ONE " : X(2) = "TWO " : X(3) = "THREE " : X(4) = "FOUR " : X(5) = "FIVE " : X(6) = "SIX " : X(7) = "SEVEN " : X(8) = "EIGHT " : X(9) = "NINE "
x1(1) = "TEN " : x1(2) = "TWENTY " : x1(3) = "THIRTY " : x1(4) = "FORTY " : x1(5) = "FIFTY " : x1(6) = "SIXTY " : x1(7) = "SEVENTY " : x1(8) = "EIGHTY " : x1(9) = "NINETY "
x2(1) = "ELEVEN " : x2(2) = "TWELVE " : x2(3) = "THIRTEEN " : x2(4) = "FOURTEEN " : x2(5) = "FIFTEEN " : x2(6) = "SIXTEEN " : x2(7) = "SEVENTEEN " : x2(8) = "EIGHTEEN " : x2(9) = "NINETEEN "


If num <= 9 Then ' for 1 to 9
WordStr = X(num)
End If
If num >= 10 Then ' for 10 to 99
If (num Mod 10 = 0) Then ' for 10,20,30....90
WordStr = x1(Int(num / 10))
End If
If (Int(num / 10) = 1 And (num Mod 10 <> 0)) Then 'for 11,12,13.....19
WordStr = x2(num Mod 10)
End If
If (Int(num / 10) > 1 And (num Mod 10 <> 0)) Then ' for 21,22,23.....99
WordStr = x1(Int(num / 10)) + X(num Mod 10)
End If
End If
End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
TextBox2.Text = NoToWord(TextBox1.Text)
End Sub

End Class


Here's a shorter code. with one RegEx and no loops. converts as you wanted, in south asian numbering system


var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

function inWords (num) {
    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})


/); if (!n) return; var str = ''; str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : ''; str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : ''; str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : ''; str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : ''; str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : ''; return str; }


这篇关于如何通过在另一个文本框中提供数值来在文本框中显示单词中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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