Hướng dẫn how do you write numbers in words in javascript? - làm thế nào để bạn viết số bằng chữ trong javascript?

Mặc dù vậy, câu hỏi này đã được trả lời - tôi vẫn muốn chia sẻ một cái gì đó mà tôi đã phát triển gần đây trong tập lệnh Java [dựa trên logic của một triển khai mạng C#cũ mà tôi tìm thấy trên Internet] để chuyển đổi các giá trị tiền tệ Ấn Độ thành từ. Nó có thể xử lý tối đa 40 chữ số. Bạn có thể xem qua.

Sử dụng: inrtowordConverter.initialize [] ;. var inwords = inrTowDonConverter.convertToword [số lượng];

Implementation:

htPunctuation = {};
listStaticSuffix = {};
listStaticPrefix = {};
listHelpNotation = {};

var InrToWordConverter = function [] {

};

InrToWordConverter.Initialize = function [] {
    InrToWordConverter.LoadStaticPrefix[];
    InrToWordConverter.LoadStaticSuffix[];
    InrToWordConverter.LoadHelpofNotation[];
};

InrToWordConverter.ConvertToWord = function [value] {
    value = value.toString[];

    if [value] {
        var tokens = value.split["."];
        var rsPart = "";
        var psPart = "";
        if [tokens.length === 2] {
            rsPart = String.trim[tokens[0]] || "0";
            psPart = String.trim[tokens[1]] || "0";
        }
        else if [tokens.length === 1] {
            rsPart = String.trim[tokens[0]] || "0";
            psPart = "0";
        }
        else {
            rsPart = "0";
            psPart = "0";
        }

        htPunctuation = {};
        var rsInWords = InrToWordConverter.ConvertToWordInternal[rsPart] || "Zero";
        var psInWords = InrToWordConverter.ConvertToWordInternal[psPart] || "Zero";

        var result = "Rupees " + rsInWords + "and " + psInWords + " Paise.";
        return result;
    }
};

InrToWordConverter.ConvertToWordInternal = function [value] {
    var convertedString = "";
    if [![value.toString[].length > 40]]
    {
        if [InrToWordConverter.IsNumeric[value.toString[]]]
        {
            try
            {
                var strValue = InrToWordConverter.Reverse[value];
                switch [strValue.length]
                {
                    case 1:
                        if [parseInt[strValue.toString[]] > 0] {
                            convertedString = InrToWordConverter.GetWordConversion[value];
                        }
                        else {
                            convertedString = "Zero ";
                        }
                        break;
                    case 2:
                        convertedString = InrToWordConverter.GetWordConversion[value];
                        break;
                    default:
                        InrToWordConverter.InsertToPunctuationTable[strValue];
                        InrToWordConverter.ReverseHashTable[];
                        convertedString = InrToWordConverter.ReturnHashtableValue[];
                        break;
                }
            }
            catch [exception] {
                convertedString = "Unexpected Error Occured 
"; } } else { convertedString = "Please Enter Numbers Only, Decimal Values Are not supported"; } } else { convertedString = "Please Enter Value in Less Then or Equal to 40 Digit"; } return convertedString; }; InrToWordConverter.IsNumeric = function [valueInNumeric] { var isFine = true; valueInNumeric = valueInNumeric || ""; var len = valueInNumeric.length; for [var i = 0; i < len; i++] { var ch = valueInNumeric[i]; if [![ch >= '0' && ch = 0; i--] { var hKey = keysArr[i]; if [InrToWordConverter.GetWordConversion[[htPunctuation[hKey]].toString[]] !== ""] { strFinalString = strFinalString + InrToWordConverter.GetWordConversion[[htPunctuation[hKey]].toString[]] + InrToWordConverter.StaticPrefixFind[[hKey].toString[]]; } } return strFinalString; }; InrToWordConverter.ReverseHashTable = function [] { var htTemp = {}; for [var key in htPunctuation] { var item = htPunctuation[key]; htTemp[key] = InrToWordConverter.Reverse[item.toString[]]; } htPunctuation = {}; htPunctuation = htTemp; }; InrToWordConverter.InsertToPunctuationTable = function [strValue] { htPunctuation[1] = strValue.substr[0, 3].toString[]; var j = 2; for [var i = 3; i < strValue.length; i = i + 2] { if [strValue.substr[i].length > 0] { if [strValue.substr[i].length >= 2] { htPunctuation[j] = strValue.substr[i, 2].toString[]; } else { htPunctuation[j] = strValue.substr[i, 1].toString[]; } } else { break; } j++; } }; InrToWordConverter.Reverse = function [strValue] { var reversed = ""; for [var i in strValue] { var ch = strValue[i]; reversed = ch + reversed; } return reversed; }; InrToWordConverter.GetWordConversion = function [inputNumber] { var toReturnWord = ""; if [inputNumber.length 0] { if [inputNumber.length === 3] { if [parseInt[inputNumber.substr[0, 1]] > 0] { toReturnWord = toReturnWord + InrToWordConverter.StaticSuffixFind[inputNumber.substr[0, 1]] + "Hundred "; } var tempString = InrToWordConverter.StaticSuffixFind[inputNumber.substr[1, 2]]; if [tempString === ""] { toReturnWord = toReturnWord + InrToWordConverter.StaticSuffixFind[inputNumber.substr[1, 1] + "0"]; toReturnWord = toReturnWord + InrToWordConverter.StaticSuffixFind[inputNumber.substr[2, 1]]; } toReturnWord = toReturnWord + tempString; } if [inputNumber.length === 2] { var tempString = InrToWordConverter.StaticSuffixFind[inputNumber.substr[0, 2]]; if [tempString === ""] { toReturnWord = toReturnWord + InrToWordConverter.StaticSuffixFind[inputNumber.substr[0, 1] + "0"]; toReturnWord = toReturnWord + InrToWordConverter.StaticSuffixFind[inputNumber.substr[1, 1]]; } toReturnWord = toReturnWord + tempString; } if [inputNumber.length === 1] { toReturnWord = toReturnWord + InrToWordConverter.StaticSuffixFind[inputNumber.substr[0, 1]]; } } return toReturnWord; }; InrToWordConverter.StaticSuffixFind = function [numberKey] { var valueFromNumber = ""; for [var key in listStaticSuffix] { if [String.trim[key.toString[]] === String.trim[numberKey]] { valueFromNumber = listStaticSuffix[key].toString[]; break; } } return valueFromNumber; }; InrToWordConverter.StaticPrefixFind = function [numberKey] { var valueFromNumber = ""; for [var key in listStaticPrefix] { if [String.trim[key] === String.trim[numberKey]] { valueFromNumber = listStaticPrefix[key].toString[]; break; } } return valueFromNumber; }; InrToWordConverter.StaticHelpNotationFind = function [numberKey] { var helpText = ""; for [var key in listHelpNotation] { if [String.trim[key.toString[]] === String.trim[numberKey]] { helpText = listHelpNotation[key].toString[]; break; } } return helpText; }; InrToWordConverter.LoadStaticPrefix = function [] { listStaticPrefix[2] = "Thousand "; listStaticPrefix[3] = "Lac "; listStaticPrefix[4] = "Crore "; listStaticPrefix[5] = "Arab "; listStaticPrefix[6] = "Kharab "; listStaticPrefix[7] = "Neel "; listStaticPrefix[8] = "Padma "; listStaticPrefix[9] = "Shankh "; listStaticPrefix[10] = "Maha-shankh "; listStaticPrefix[11] = "Ank "; listStaticPrefix[12] = "Jald "; listStaticPrefix[13] = "Madh "; listStaticPrefix[14] = "Paraardha "; listStaticPrefix[15] = "Ant "; listStaticPrefix[16] = "Maha-ant "; listStaticPrefix[17] = "Shisht "; listStaticPrefix[18] = "Singhar "; listStaticPrefix[19] = "Maha-singhar "; listStaticPrefix[20] = "Adant-singhar "; }; InrToWordConverter.LoadStaticSuffix = function [] { listStaticSuffix[1] = "One "; listStaticSuffix[2] = "Two "; listStaticSuffix[3] = "Three "; listStaticSuffix[4] = "Four "; listStaticSuffix[5] = "Five "; listStaticSuffix[6] = "Six "; listStaticSuffix[7] = "Seven "; listStaticSuffix[8] = "Eight "; listStaticSuffix[9] = "Nine "; listStaticSuffix[10] = "Ten "; listStaticSuffix[11] = "Eleven "; listStaticSuffix[12] = "Twelve "; listStaticSuffix[13] = "Thirteen "; listStaticSuffix[14] = "Fourteen "; listStaticSuffix[15] = "Fifteen "; listStaticSuffix[16] = "Sixteen "; listStaticSuffix[17] = "Seventeen "; listStaticSuffix[18] = "Eighteen "; listStaticSuffix[19] = "Nineteen "; listStaticSuffix[20] = "Twenty "; listStaticSuffix[30] = "Thirty "; listStaticSuffix[40] = "Fourty "; listStaticSuffix[50] = "Fifty "; listStaticSuffix[60] = "Sixty "; listStaticSuffix[70] = "Seventy "; listStaticSuffix[80] = "Eighty "; listStaticSuffix[90] = "Ninty "; }; InrToWordConverter.LoadHelpofNotation = function [] { listHelpNotation[2] = "=1,000 [3 Trailing Zeros]"; listHelpNotation[3] = "=1,00,000 [5 Trailing Zeros]"; listHelpNotation[4] = "=1,00,00,000 [7 Trailing Zeros]"; listHelpNotation[5] = "=1,00,00,00,000 [9 Trailing Zeros]"; listHelpNotation[6] = "=1,00,00,00,00,000 [11 Trailing Zeros]"; listHelpNotation[7] = "=1,00,00,00,00,00,000 [13 Trailing Zeros]"; listHelpNotation[8] = "=1,00,00,00,00,00,00,000 [15 Trailing Zeros]"; listHelpNotation[9] = "=1,00,00,00,00,00,00,00,000 [17 Trailing Zeros]"; listHelpNotation[10] = "=1,00,00,00,00,00,00,00,00,000 [19 Trailing Zeros]"; listHelpNotation[11] = "=1,00,00,00,00,00,00,00,00,00,000 [21 Trailing Zeros]"; listHelpNotation[12] = "=1,00,00,00,00,00,00,00,00,00,00,000 [23 Trailing Zeros]"; listHelpNotation[13] = "=1,00,00,00,00,00,00,00,00,00,00,00,000 [25 Trailing Zeros]"; listHelpNotation[14] = "=1,00,00,00,00,00,00,00,00,00,00,00,00,000 [27 Trailing Zeros]"; listHelpNotation[15] = "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,000 [29 Trailing Zeros]"; listHelpNotation[16] = "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000 [31 Trailing Zeros]"; listHelpNotation[17] = "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000 [33 Trailing Zeros]"; listHelpNotation[18] = "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000 [35 Trailing Zeros]"; listHelpNotation[19] = "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000 [37 Trailing Zeros]"; listHelpNotation[20] = "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000 [39 Trailing Zeros]"; }; if [!String.trim] { String.trim = function [str] { var result = ""; var firstNonWhiteSpaceFound = false; var startIndex = -1; var endIndex = -1; if [str] { for [var i = 0; i < str.length; i++] { if [firstNonWhiteSpaceFound === false] { if [str[i] === ' ' || str[i] === '\t'] { continue; } else { firstNonWhiteSpaceFound = true; startIndex = i; endIndex = i; } } else { if [str[i] === ' ' || str[i] === '\t'] { continue; } else { endIndex = i; } } } if [startIndex !== -1 && endIndex !== -1] { result = str.slice[startIndex, endIndex + 1]; } } return result; }; }

Làm cách nào để chuyển đổi số thành từ?

Sử dụng hàm SpellNumber trong các ô riêng lẻ Nhập công thức = SpellNumber [A1] vào ô nơi bạn muốn hiển thị một số bằng văn bản, trong đó A1 là ô chứa số bạn muốn chuyển đổi. Bạn cũng có thể gõ thủ công giá trị như = SpellNumber [22,50]. Nhấn Enter để xác nhận công thức. Type the formula =SpellNumber[A1] into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber[22.50]. Press Enter to confirm the formula.

Làm thế nào được chuyển đổi thành các chuỗi trong JavaScript?

Phương thức toString [] trong javascript được sử dụng với một số và chuyển đổi số thành một chuỗi. Nó được sử dụng để trả về một chuỗi đại diện cho đối tượng số được chỉ định. Phương thức toString [] được sử dụng với một số num như thể hiện trong cú pháp trên bằng cách sử dụng '. ' nhà điều hành. in Javascript is used with a number and converts the number to a string. It is used to return a string representing the specified Number object. The toString[] method is used with a number num as shown in the above syntax using the '. ' operator.

Từ khóa số trong JavaScript là gì?

Số là một đối tượng trình bao bọc nguyên thủy được sử dụng để biểu diễn và thao tác các số như 37 hoặc -9,25.Chất xây dựng số chứa các hằng số và phương pháp để làm việc với các số.Các giá trị của các loại khác có thể được chuyển đổi thành số bằng hàm số [].a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25 . The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number[] function.

Làm thế nào để bạn hiển thị một số trong JavaScript?

Trong JavaScript, một số có thể là một giá trị nguyên thủy [typeOf = number] hoặc một đối tượng [typeof = object].Phương thức valueOf [] được sử dụng nội bộ trong JavaScript để chuyển đổi các đối tượng số thành các giá trị nguyên thủy.Không có lý do để sử dụng nó trong mã của bạn.Tất cả các loại dữ liệu JavaScript có một giá trị [] và phương thức toString [].a number can be a primitive value [typeof = number] or an object [typeof = object]. The valueOf[] method is used internally in JavaScript to convert Number objects to primitive values. There is no reason to use it in your code. All JavaScript data types have a valueOf[] and a toString[] method.

Bài Viết Liên Quan

Chủ Đề