我需要一个R语言的函数来将数量转换为crore, lakh, thousand etc ...
中的印度单词例如:
功能(3257)应该产生:三千二百五十七;
功能(473257)应该产生:四个Lakh七十二千五百七十七;
功能(12473257)应该产生:One Crore Twenty Four Lakh七十二千五百七十七
可以在互联网上找到大量可用的VBA功能,用于Microsoft Excel和Access,但我无法在R语言中找到类似的功能。
修改:如何用英文单词做出示例:https://github.com/ateucher/useful_code/blob/master/R/numbers2words.r
答案 0 :(得分:0)
似乎我已经弄明白了。感兴趣的用户可以测试并报告错误(如果有的话)。
# 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"
答案 1 :(得分:0)
将数字量转换为印度标准的数字(最多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;
}