Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

i require a C# function which convert amount to words for thai language.

example:
1,450,829.77 => หนึ่งล้านสี่แสนห้าหมื่นแปดร้อยยี่สิบเก้าบาทและเจ็ดสิบเจ็ดสตางค์
30,459.24 => สามหมื่นสี่ร้อยห้าสิบเก้าบาทและยี่สิบสี่สตางค์

Thanx in Advance

What I have tried:

Currently i have created in SQL Database but its giving some wrong output, its not giving as per above example

SQL
ALTER FUNCTION [dbo].[fnNumberToThaiWords](@Number as BIGINT)

    RETURNS NVARCHAR(1024)

AS

BEGIN

      DECLARE @Below20 TABLE (ID int identity(0,1), Word NVARCHAR(32))

      DECLARE @Below100 TABLE (ID int identity(2,1), Word NVARCHAR(32))

      INSERT @Below20 (Word) VALUES
                        ( N'ศูนย์'), (N'หนึ่ง'),(N'สอง' ), (N'สาม'),
                        ( N'สี่' ), ( N'ห้า' ), ( N'หก' ), ( N'เจ็ด' ),
                        ( N'แปด'), ( N'เก้า'), ( N'สิบ'), ( N'สิบเอ็ด' ),
                        ( N'สิบสอง' ), ( N'สิบสาม' ), ( N'สิบสี่'),
                        ( N'สิบห้า' ), (N'สิบหก' ), ( N'สิบเจ็ด'),
                        (N'สิบแปด' ), ( N'สิบเก้า' )
      
	   INSERT @Below100 VALUES (N'ยี่สิบ'), (N'สามสิบ'),(N'สี่สิบ'), (N'ห้าสิบ'),
                               (N'หกสิบ'), (N'เจ็ดสิบ'), (N'แปดสิบ'), (N'เก้าสิบ')

DECLARE @English Nvarchar(1024) =
(
  SELECT Case

    WHEN @Number = 0 THEN  ''

    WHEN @Number BETWEEN 1 AND 19
      THEN (SELECT Word FROM @Below20 WHERE ID=@Number)

   WHEN @Number BETWEEN 20 AND 99  
     THEN  (SELECT Word FROM @Below100 WHERE ID=@Number/10) + dbo.fnNumberToThaiWords( @Number % 10)

   WHEN @Number BETWEEN 100 AND 999  
     THEN  (dbo.fnNumberToThaiWords( @Number / 100))+N'ร้อย' + dbo.fnNumberToThaiWords( @Number % 100)

   WHEN @Number BETWEEN 1000 AND 999999
     THEN  (dbo.fnNumberToThaiWords( @Number / 1000))+N'พัน' + dbo.fnNumberToThaiWords( @Number % 1000) 

   WHEN @Number BETWEEN 1000000 AND 999999999  
     THEN  (dbo.fnNumberToThaiWords( @Number / 1000000))+N'ล้าน' + dbo.fnNumberToThaiWords( @Number % 1000000)

   WHEN @Number BETWEEN 1000000000 AND 999999999999  
     THEN  (dbo.fnNumberToThaiWords( @Number / 1000000000))+N'พันล้าน'+ dbo.fnNumberToThaiWords( @Number % 1000000000)

   WHEN @Number BETWEEN 1000000000000 AND 999999999999999  
     THEN  (dbo.fnNumberToThaiWords( @Number / 1000000000000))+N'ล้านล้าน'+ dbo.fnNumberToThaiWords( @Number % 1000000000000)

  --WHEN @Number BETWEEN 1000000000000000 AND 999999999999999999  

  --   THEN  (dbo.fnNumberToThaiWords( @Number / 1000000000000000))+' Quadrillion '+

  --       dbo.fnNumberToThaiWords( @Number % 1000000000000000)

  --WHEN @Number BETWEEN 1000000000000000000 AND 999999999999999999999  

  --   THEN  (dbo.fnNumberToThaiWords( @Number / 1000000000000000000))+' Quintillion '+

  --       dbo.fnNumberToThaiWords( @Number % 1000000000000000000)

        ELSE N'ข้อมูลที่ไม่ถูกต้อง' END

)



SELECT @English = RTRIM(@English)
SELECT @English = RTRIM(LEFT(@English,len(@English)-1))
                 WHERE RIGHT(@English,1)='-'
RETURN (@English)

END	
Posted
Updated 5-Oct-18 3:08am
v4
Comments
Richard MacCutchan 5-Oct-18 4:21am    
You have the SQL code, so converting it to C# should not be a problem.
Member 13934994 5-Oct-18 5:16am    
Hello,
this is not correct code.
Richard MacCutchan 5-Oct-18 5:29am    
Then throw it away and start again. Converting numbers to words (in any language) is not a difficult problem, and Google will most likely find you some ready made articles on the subject.
Member 13934994 5-Oct-18 6:16am    
yes, every one easily can find amount in words for english, but its difficult for thai.
we also found some function and above sql query also created but client told that its giving wrong output
Richard MacCutchan 5-Oct-18 6:18am    
However difficult, it will still be easier in C# than SQL. And Having the code inside the SQL server is really a waste of resources. The only time you need it converted is when you want to display it for a human to read.

1 solution

While it's entirely possible to make SQL server do this, it's not an appropriate platform for the work. Retrieve the data into a C# application, and translate it there.

FWIW, calling a function from a query is the best way I know of to significantly reduce the speed of the query. C# is MUCH better suited for the task.
 
Share this answer
 
Comments
Member 13934994 5-Oct-18 10:00am    
Hello,
yes right c# function is better but i tried so much to find out amount in words function in thai.
please provide function if you can search
#realJSOP 5-Oct-18 10:11am    
I don't speak thai, and besides, I'm not at all familiar with numeric organization in that language, so I cannot possibly help you. I did write some code that converts text to its integer numeric equivalent (example: "one hundred and five" converts to 105). The conversion doesn't go the other way, but it should be a minor effort for an interested C# programmer to implement.

https://www.codeproject.com/Articles/875581/Converting-Text-Numbers-to-Numeric-Values

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900