函数将金额转换为R语言中的印度文字(克罗尔、拉克斯、千…)

函数将金额转换为R语言中的印度文字(克罗尔、拉克斯、千…),r,R,我需要一个在R语言的函数转换成印度单词的金额在 例如: 函数(3257)应生成:叁仟贰佰伍拾柒 函数(473257)应生成:肆拾柒万叁仟贰佰伍拾柒 功能(12473257)应产生:一亿两千四百七十三万三千二百五十七 在internet上可以找到很多可以与Microsoft Excel和Access一起使用的VBA函数,但我在R语言中找不到类似的函数 编辑:示例如何用英语进行编辑:看来我自己已经解决了。感兴趣的用户可以测试并报告bug(如果有) # function to convert nu

我需要一个在R语言的函数转换成印度单词的金额在

例如:

  • 函数(3257)应生成:叁仟贰佰伍拾柒

  • 函数(473257)应生成:肆拾柒万叁仟贰佰伍拾柒

  • 功能(12473257)应产生:一亿两千四百七十三万三千二百五十七

在internet上可以找到很多可以与Microsoft Excel和Access一起使用的VBA函数,但我在R语言中找不到类似的函数


编辑:示例如何用英语进行编辑:

看来我自己已经解决了。感兴趣的用户可以测试并报告bug(如果有)

# function to convert number to words in Indian counting system
# https://en.wikipedia.org/wiki/Indian_numbering_system#Names_of_numbers

# number always rounded; multiple numbers also accepted as vector
# currently handles numbers upto 15 digits but can be easily extended

# Credit: A big THANKS to Mr. John Fox.
# His function to convert number to English words can be found at:
# http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html

SpellIndianNumber <- function(x){

  helper <- function(x){

    digits <- rev(strsplit(as.character(x), "")[[1]])
    nDigits <- length(digits)

    # function meant to handle numbers upto 15 digits only
    if(nDigits > 15) stop("Number is too large!")

    # single digit cases: 1 to 9
    if (nDigits == 1) as.vector(ones[digits])

    # two digits cases: 10 to 99
    else if (nDigits == 2)
      if (x <= 19) as.vector(teens[digits[1]]) # for 10 to 19
    else trim(paste(tens[digits[2]], # for 20 to 99
                    Recall(as.numeric(digits[1]))))

    # three digits cases: 100 to 999
    else if (nDigits == 3) trim(paste(ones[digits[3]], "Hundred", 
                                      Recall(makeNumber(digits[2:1]))))

    # four & five digits cases: handling thousands
    else if (nDigits <= 5){
      thousands <- x %/% 1e3
      remainder <- x %% 1e3
      trim(paste(Recall(thousands), "Thousand", Recall(remainder)))
    }

    # six & seven digits cases: handling lakhs
    else if (nDigits <= 7){
      lakhs <- x %/% 1e5
      remainder <- x %% 1e5
      trim(paste(Recall(lakhs), "Lakh", Recall(remainder)))
    }

    # eight & nine digits cases: handling crores
    else if (nDigits <= 9){
      crores <- x %/% 1e7
      remainder <- x %% 1e7
      trim(paste(Recall(crores), "Crore", Recall(remainder)))
    }

    # ten & eleven digits cases: handling arabs
    else if (nDigits <= 11){
      arabs <- x %/% 1e9
      remainder <- x %% 1e9
      trim(paste(Recall(arabs), "Arab", Recall(remainder)))
    }

    # twelve & thirteen digits cases: handling kharabs
    else if (nDigits <= 13){
      kharabs <- x %/% 1e11
      remainder <- x %% 1e11
      trim(paste(Recall(kharabs), "Kharab", Recall(remainder)))
    }

    # fourteen & fifteen digits cases: handling neels
    else if (nDigits <= 15){
      neels <- x %/% 1e13
      remainder <- x %% 1e13
      trim(paste(Recall(neels), "Neel", Recall(remainder)))
    }
  }

  trim <- function(text){
    gsub("^\ ", "", gsub("\ *$", "", text))
  }

  makeNumber <- function(...) as.numeric(paste(..., collapse=""))

  opts <- options(scipen=100)
  on.exit(options(opts))

  ones <- c("", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine")
  names(ones) <- 0:9

  teens <- c("Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
             "Sixteen", " Seventeen", "Eighteen", "Nineteen")
  names(teens) <- 0:9

  tens <- c("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
            "Eighty", "Ninety")
  names(tens) <- 2:9

  x <- round(x)

  if (length(x) > 1) return(sapply(x, helper))
  helper(x)
}

# Examples:

# > SpellIndianNumber(83720834)
# [1] "Eight Crore Thirty Seven Lakh Twenty Thousand Eight Hundred Thirty Four"

# > SpellIndianNumber(1283720834)
# [1] "One Arab Twenty Eight Crore Thirty Seven Lakh Twenty Thousand Eight Hundred Thirty Four"

# > SpellIndianNumber(653234532342345)
# [1] "Sixty Five Neel Thirty Two Kharab Thirty Four Arab Fifty Three Crore Twenty Three Lakh Forty Two Thousand Three Hundred Forty Five"

# > SpellIndianNumber(c(5,10,87))
# [1] "Five"         "Ten"          "Eighty Seven"

# > SpellIndianNumber(11:15)
# [1] "Eleven"   "Twelve"   "Thirteen" "Fourteen" "Fifteen" 

# Number Zero will appear as a zero length string
# > SpellIndianNumber(0)
# [1] ""

# > SpellIndianNumber(c(12,0,87))
# [1] "Twelve"       ""             "Eighty Seven"
#在印度计数系统中将数字转换为文字的函数
# https://en.wikipedia.org/wiki/Indian_numbering_system#Names_of_numbers
#数字总是四舍五入;多个数字也被接受为向量
#目前可处理高达15位的数字,但可以轻松扩展
#信贷:非常感谢约翰·福克斯先生。
#其将数字转换为英语单词的功能可在以下网址找到:
# http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html

SpellIndianNumber将数字金额转换为印度标准的数字,最多12位

function GetAmountInWords(Amount) {
    var nums = parseFloat(Amount).toFixed(2).toString().split('.');
    var AmountInWords = '';
    if (nums.length > 0 && parseInt(nums[0]) > 0) {
    AmountInWords += ReturnAmountInWords(nums[0]) + 'Rupees '
    }
    if (nums.length == 2 && parseInt(nums[1]) > 0) {
        var Paisa = ReturnAmountInWords(nums[1]);
        AmountInWords += ((AmountInWords.trim().length > 0) ? 'and ' : '') + Paisa + 'Paisa ';
    }
    return (AmountInWords.length > 0) ? AmountInWords + 'Only.' : '';
}

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 ReturnAmountInWords(num) {
    if ((num = num.toString()).length > 12) return 'overflow';
    n = ('000000000000' + num).substr(-12).match(/^(\d{2})(\d{1})(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return;
    var AmountStr = '';
    AmountStr += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'Thousand ' : '';
    AmountStr += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'Hundred ' : '';
    AmountStr += (n[1] != 0 || n[2] != 0 || n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'Crore ' : '';
    AmountStr += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'Lakh ' : '';
    AmountStr += (n[5] != 0) ? (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'Thousand ' : '';
    AmountStr += (n[6] != 0) ? (a[Number(n[6])] || b[n[6][0]] + ' ' + a[n[6][1]]) + 'Hundred ' : '';
    AmountStr += (n[7] != 0) ? ((AmountStr != '') ? 'and ' : '') + (a[Number(n[7])] || b[n[7][0]] + ' ' + a[n[7][1]]) + '' : '';
    return AmountStr;
}

看看
english::as.english
。显然,您需要编辑代码