Number to Words Function

This is a set of codes that can convert text from TextInput into Word Description.

If you want to use it in your local language just replace the labels in the arrays.

Just add the code below to the Text Property of your label.

With({
    _val: Value(TextInput1.Text),
    _text: Text(Value(TextInput1.Text),"000 000 000 000 000.00"),
    _maxPlace: RoundUp(Len(Text(Value(TextInput1.Text),"0"))/3,0)+1,
    _places: [" Trillion "," Billion "," Million "," Thousand "," Pesos and "," Cents"],
    _ones: ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
    _teens: ["Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"],
    _tens: ["Ten","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
},
    With({
        _vals: With({
                    XAll: LastN(Split(_text," "),_maxPlace-1)
                },
                    Filter(Ungroup(
                            Table(
                            {NumX: If(_maxPlace>2,FirstN(XAll,_maxPlace-2),Blank())},
                            {NumX: Split(Last(XAll).Value,".")}
                            ),
                            "NumX"
                        )
                    ,!IsBlank(ThisRecord.Value))
                ),
        _selPlace: LastN(_places,_maxPlace)
    },

	With({
		_TempTable: ForAll(Sequence(_maxPlace,1,1) As X ,
            {
                	_Amount: Value(Last(FirstN(_vals,X.Value)).Value),
                	_Place: Last(FirstN(_selPlace,X.Value)).Value,
                	_Actual: _val
            }
        )

	},
		With({
            _ABC:
            ForAll(_TempTable As Y,
                With(
                    {
                        _mod10: Mod(Y._Amount, 10), 
                        _mod100: Mod(Y._Amount, 100),
                        _HUNDREDS: Value(Left(Y._Amount, 1)),
                        _TENS: Value(Left(Mod(Y._Amount, 100),1))

                    },
                    {
                        Ress:   If(Y._Actual = 0, "Zero",
                                If((Y._Amount = 0 Or IsBlank(Y._Amount)) And Y._Place = " Cents", "Zero",
                                    If(Y._Amount >= 100,
                                            Concatenate(
                                                Last(FirstN(_ones,_HUNDREDS)).Value,
                                                " Hundred",
                                                If(_mod100>0, " and ", "")
                                            )
                                        )
                                    &
                                    If(_mod100 >= 20,
                                        Concatenate(
                                            Last(FirstN(_tens, _TENS)).Value,
                                            If(_mod10 > 0, "-" & Last(FirstN(_ones,_mod10)).Value, "")
                                        ),
                                        If(_mod100 > 10,
                                            Last(FirstN(_teens,_mod10)).Value,
                                            If(_mod100=0,"",
                                            If(_mod10=0,"Ten",Last(FirstN(_ones,_mod10)).Value))
                                        )
                                    )
                                ))
                        & Y._Place
                    }
                )
            )
            }
            ,
        Concat(_ABC,Ress,"")
        )

	)
        
    )
)
JavaScript

Here are some code to check on the value. Will add more detailed description about it later.

This one is checking it gallery view. Add this as item code:

With({
    _val: Value(TextInput1.Text),
    _text: Text(Value(TextInput1.Text),"000 000 000 000 000.00"),
    _maxPlace: RoundUp(Len(Text(Value(TextInput1.Text),"0"))/3,0)+1,
    _places: [" Trillion "," Billion "," Million "," Thousand "," Pesos and "," Cents"],
    _ones: ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
    _teens: ["Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"],
    _tens: ["Ten","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
},
    With({
        _vals: With({
                    XAll: LastN(Split(_text," "),_maxPlace-1)
                },
                    Filter(Ungroup(
                            Table(
                            {NumX: If(_maxPlace>2,FirstN(XAll,_maxPlace-2),Blank())},
                            {NumX: Split(Last(XAll).Value,".")}
                            ),
                            "NumX"
                        )
                    ,!IsBlank(ThisRecord.Value))
                ),
        _selPlace: LastN(_places,_maxPlace)
    },
        ForAll(Sequence(_maxPlace,1,1) As X ,
            {
                Value: Value(Last(FirstN(_vals,X.Value)).Value),
                Place: Last(FirstN(_selPlace,X.Value)).Value,
                Actual: _val
            }
        )
    )
)
JavaScript

This one is the basic code for creating description on a 3 digit number which is the primary component of this function.

With({
	_ones: ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
	_teens: ["Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"],
	_tens: ["Ten","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"],
    _amount: Value(TextInput1.Text), 
    _mod10: Mod(Value(TextInput1.Text), 10), 
    _mod100: Mod(Value(TextInput1.Text), 100)
    
},
    With({
        _HUNDREDS: Value(Left(_amount, 1)),
        _TENS: Value(Left(_mod100,1))

    },
        If(_amount = 0, "Zero",
            If(_amount >= 100,
                    Concatenate(
                        Last(FirstN(_ones,_HUNDREDS)).Value,
                        " Hundred",
                        If(_mod100>0, " and ", "")
                    )
                )
            &
            If(_mod100 >= 20,
                Concatenate(
                    Last(FirstN(_tens, _TENS)).Value,
                    If(_mod10 > 0, "-" & Last(FirstN(_ones,_mod10)).Value, "")
                ),
                If(_mod100 > 10,
                    Last(FirstN(_teens,_mod10)).Value,
                    If(_mod100=0,"",
                    If(_mod10=0,"Ten",Last(FirstN(_ones,_mod10)).Value))
                )
            )
        )
    )
)
JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *