在Google Sheets中将数字转换为文本/文字

5

例子:

数值 期望输出
300 三百
300.50 三百元五角

我无法在Google Sheets中找到方法来完成这个任务,而且我不够精通去构建任何东西。

7个回答

11
这个网站展示了一个可以实现此功能的Excel公式https://www.excelforum.com/tips-and-tutorials/1015010-convert-a-numeric-value-to-words-without-vba.html。我刚刚在Google Sheets中测试过,也可以使用。
该公式为:
=IF(OR(LEN(FLOOR(E3,1))>=13,FLOOR(E3,1)<=0),"Out of range",PROPER(SUBSTITUTE(CONCATENATE(CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),1,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),2,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),3,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),2,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),3,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),2,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),3,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),IF(E3>=10^9," billion ",""),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),4,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),5,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),6,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),5,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),6,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),5,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),6,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),4,3))>0," million ",""),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),7,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),8,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),9,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),8,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),9,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),8,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),9,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),7,3))," thousand ",""),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),10,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),11,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),12,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),11,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),12,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),11,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),12,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),""))),"  "," ")&IF(FLOOR(E3,1)>1," dollars"," dollar")))

5

Dominic的回答进行微调,以满足您的特定需求:

=REGEXREPLACE(IF(OR(LEN(FLOOR(A1,1))>=13,FLOOR(A1,1)<=0),"Out of range",
 PROPER(SUBSTITUTE(CONCATENATE(CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),1,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
 CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),2,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),3,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),2,1))>1,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),3,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),2,1))=0,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),3,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),
 IF(A1>=10^9," billion ",""),
 CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),4,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
 CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),5,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),6,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),5,1))>1,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),6,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),5,1))=0,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),6,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),4,3))>0," million ",""),CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),7,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
 CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),8,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),9,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),8,1))>1,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),9,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),8,1))=0,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),9,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),7,3))," thousand ",""),CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),10,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
 CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),11,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),12,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
 IF(VALUE(MID(TEXT(INT(A1), 
 REPT(0,12)),11,1))>1,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),12,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
 IF(VALUE(MID(TEXT(INT(A1),
 REPT(0,12)),11,1))=0,CHOOSE(MID(TEXT(INT(A1),
 REPT(0,12)),12,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),""))),"  "," "))&
 IF(IFERROR(QUERY({A1},"where Col1 Contains '.'"),)<>"",
 " and "&REGEXEXTRACT(TO_TEXT(A1), "\.(.+)")&" cents", )),"  ", " ")

0


你的编辑输出也包括小数位,对吗? - Khan
@Khan 是的,确实如此。 - player0

3

由于这是专门为Google Sheet设计的,您可以使用此功能。

=REGEXEXTRACT(JOIN(", ",QUERY(IMPORTDATA("https://www.calculatorsoup.com/calculators/conversions/numberstowords.php?number=" & A1 & "&format=words&letter_case=lowercase&action=solve"),"where Col1 contains '<div id=""answer""><br>'")),">([^<]+)")

其中A1是您想要转换的数字

来源:拼写数字公式或数字转文字。该链接中还有许多替代方案,例如这里有一种使用数组公式的方法。

=ArrayFormula(if(A2:A="","",if(len(int(A2:A))<13,"",if(right(left(int(A2:A),len(int(A2:A))-12),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-12),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-12),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-12),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-12),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-12),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" trillion")&" "&if(len(int(A2:A))<10,"",if(right(left(int(A2:A),len(int(A2:A))-9),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-9),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-9),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-9),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-9),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-9),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" billion")&" "&if(len(int(A2:A))<7,"",if(right(left(int(A2:A),len(int(A2:A))-6),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-6),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-6),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-6),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-6),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-6),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" million")&" "&if(len(int(A2:A))<4,"",if(right(left(int(A2:A),len(int(A2:A))-3),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-3),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-3),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-3),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-3),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-3),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" thousand")&" "&if(len(int(A2:A))<1,"",if(right(left(int(A2:A),len(int(A2:A))-0),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-0),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-0),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-0),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-0),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-0),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")))&if(int(A2:A)=0,"0 dollar",if(int(A2:A)<2," dollar"," dollars"))&iferror(" and "&if(round(mod(A2:A,1)*100,2)>19,choose(left(mod(A2:A,1)*100)-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(round(mod(A2:A,1)*100,2))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(round(mod(A2:A,1)*100,2)+1,"no","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" cents")))

输出为
300         three hundred  dollars and no cents
300.5       three hundred  dollars and fifty cents

Array formula for number conversion example

如果您的数字位于不同位置,请将数组公式中的 A2:A 更改为正确位置


另一种方法是通过选择“文件>制作副本”来克隆以下两个已经制作好的表格,然后使用。

来源:使用 Google Sheets 将金额或数字转换为文字。作者还介绍了只需一个单独工作表的更新版本。如果您想使用这些,请从此处克隆

Number to word converter


2
你可以尝试使用 数字文本插件
=NUMBERTEXT("EUR 25") => twenty-five euro

=MONEYTEXT(25,"EUR") => twenty-five euro

0

您可以使用此方法将货币转换为文字:

function convertToWords(input) {

  var a, b, c, d, e, output, outputA, outputB, outputC, outputD, outputE;

  var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

  if (input === 0) { // Zero

    output = "Rupees zero";

  } else if (input == 1) { // One

    output = "Rupee one only";

  } else { // More than one

    // Tens
    a = input % 100;
    outputA = oneToHundred_(a);

    // Hundreds
    b = Math.floor((input % 1000) / 100);
    if (b > 0 && b < 10) {
      outputB = ones[b];
    }

    // Thousands
    c = (Math.floor(input / 1000)) % 100;
    outputC = oneToHundred_(c);

    // Lakh
    d = (Math.floor(input / 100000)) % 100;
    outputD = oneToHundred_(d);

    // Crore
    e = (Math.floor(input / 10000000)) % 100;
    outputE = oneToHundred_(e);

    // Make string
    output = "Rupees";

    if (e > 0) {
      output = output + " " + outputE + " crore";
    }

    if (d > 0) {
      output = output + " " + outputD + " lakh";
    }

    if (c > 0) {
      output = output + " " + outputC + " thousand";
    }

    if (b > 0) {
      output = output + " " + outputB + " hundred";
    }

    if (input > 100 && a > 0) {
      output = output + " and";
    }

    if (a > 0) {
      output = output + " " + outputA;
    }

    output = output + " only";
  }

  return output;

}

function oneToHundred_(num) {

  var outNum;

  var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

  var teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];

  var tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

  if (num > 0 && num < 10) { // 1 to 9

    outNum = ones[num]; // ones

  } else if (num > 9 && num < 20) { // 10 to 19

    outNum = teens[(num % 10)]; // teens

  } else if (num > 19 && num < 100) { // 20 to 100

    outNum = tens[Math.floor(num / 10)]; // tens

    if (num % 10 > 0) {

      outNum = outNum + " " + ones[num % 10]; // tens + ones

    }

  }

  return outNum;

}


0
  • 复制此文件:converter
  • 然后将这两个表格添加到您的电子表格中,或使用IMPORTRANGE将值导入到F6,并使用另一个IMPORTRANGE将转换后的值从B11导回。

0

更新

在某些情况下,提出的解决方案显示错误的结果。需要进行更多测试。


请尝试:

=GOOGLETRANSLATE(BAHTTEXT(7245.01),"th","en")

结果为:

七千二百四十五铢,一分

enter image description here

然后使用正则表达式或文本函数来消除/替换这个:

  • baht → 转换为 $ 或货币
  • satang → 转换为分或其他。

以下是翻译语言代码列表:

https://www.loc.gov/standards/iso639-2/php/code_list.php


2
我得到了“十万零二十四千四百五十一”,这是错误的。 - Markus von Broady
2
@Markus von Broady 感谢测试,当我有一些错误的一般逻辑时,我会编辑我的答案,现在我猜测这是一个错误,因为BAHTTEXT目前是一个已记录的函数。 - Max Makhrov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接