请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

张晓楠

中级会员

43

主题

104

帖子

637

积分

中级会员

积分
637
张晓楠
中级会员   /  发表于:2025-6-26 15:50  /   查看:58  /  回复:2
10金币


把这种的翻译成 USD SEVEN THOUSAND FIVE HUNDRED, AND JPY TWO MILLION
这个怎么实现
附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册

最佳答案

查看完整内容

效果: 步骤一:让AI写JS函数 步骤二:定义数值单元格名称myval,文本单元格名称myvaltext 步骤三:添加按钮命令 效果: 扩展: 1、页面加载就翻译,将命令放到页面加载命令中执行 2、多金额,多币种

2 个回复

倒序浏览
最佳答案
最佳答案
amtath悬赏达人认证 活字格认证
论坛元老   /  发表于:2025-6-26 15:50:41
来自 2#
本帖最后由 amtath 于 2025-6-26 22:47 编辑


效果:


步骤一:让AI写JS函数



步骤二:定义数值单元格名称myval,文本单元格名称myvaltext


步骤三:添加按钮命令

  1. //从myval单元格获取数值
  2. var myval=Forguncy.Page.getCell("myval").getValue();

  3. //调用下方numberToWords函数,进行翻译
  4. var myvalstr=numberToWords(myval);

  5. //设置单元格命令,将翻译好的结果赋值给myvaltext单元格
  6. Forguncy.Page.getCell("myvaltext").setValue(myvalstr);

  7. function numberToWords(num) {
  8.     if (num === 0) return 'zero';
  9.    
  10.     const units = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  11.     const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
  12.     const tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
  13.     const scales = ['', 'thousand', 'million', 'billion', 'trillion'];
  14.    
  15.     let words = [];
  16.     let scaleIndex = 0;
  17.    
  18.     while (num > 0) {
  19.         let chunk = num % 1000;
  20.         num = Math.floor(num / 1000);
  21.         
  22.         if (chunk !== 0) {
  23.             let chunkWords = [];
  24.             
  25.             // 处理百位
  26.             if (chunk >= 100) {
  27.                 chunkWords.push(units[Math.floor(chunk / 100)] + ' hundred');
  28.                 chunk %= 100;
  29.             }
  30.             
  31.             // 处理十位和个位
  32.             if (chunk >= 20) {
  33.                 chunkWords.push(tens[Math.floor(chunk / 10)]);
  34.                 chunk %= 10;
  35.                 if (chunk > 0) {
  36.                     chunkWords.push(units[chunk]);
  37.                 }
  38.             } else if (chunk >= 10) {
  39.                 chunkWords.push(teens[chunk - 10]);
  40.             } else if (chunk > 0) {
  41.                 chunkWords.push(units[chunk]);
  42.             }
  43.             
  44.             if (scaleIndex > 0) {
  45.                 chunkWords.push(scales[scaleIndex]);
  46.             }
  47.             
  48.             words.unshift(chunkWords.join(' '));
  49.         }
  50.         
  51.         scaleIndex++;
  52.     }
  53.    
  54.     return words.join(' ');
  55. }
复制代码


效果:




扩展:


1、页面加载就翻译,将命令放到页面加载命令中执行

2、多金额,多币种




  1. var myval=Forguncy.Page.getCell("myval").getValue();
  2. var myval2=Forguncy.Page.getCell("myval2").getValue();
  3. var bibie=Forguncy.Page.getCell("bibie").getValue();
  4. var bibie2=Forguncy.Page.getCell("bibie2").getValue();


  5. var myvalstr=currencyToWords(bibie,myval,bibie2,myval2);


  6. Forguncy.Page.getCell("myvaltext").setValue(myvalstr);
复制代码


  1. function currencyToWords(currency1, amount1, currency2, amount2) {
  2.     function numberToWords(num) {
  3.         if (num === 0) return 'zero';
  4.         
  5.         const units = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  6.         const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
  7.                       'seventeen', 'eighteen', 'nineteen'];
  8.         const tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy',
  9.                      'eighty', 'ninety'];
  10.         const scales = ['', 'thousand', 'million', 'billion', 'trillion'];
  11.         
  12.         let words = [];
  13.         let scaleIndex = 0;
  14.         
  15.         while (num > 0) {
  16.             let chunk = num % 1000;
  17.             num = Math.floor(num / 1000);
  18.             
  19.             if (chunk !== 0) {
  20.                 let chunkWords = [];
  21.                
  22.                 if (chunk >= 100) {
  23.                     chunkWords.push(units[Math.floor(chunk / 100)] + ' hundred');
  24.                     chunk %= 100;
  25.                 }
  26.                
  27.                 if (chunk >= 20) {
  28.                     chunkWords.push(tens[Math.floor(chunk / 10)]);
  29.                     chunk %= 10;
  30.                     if (chunk > 0) {
  31.                         chunkWords.push(units[chunk]);
  32.                     }
  33.                 } else if (chunk >= 10) {
  34.                     chunkWords.push(teens[chunk - 10]);
  35.                 } else if (chunk > 0) {
  36.                     chunkWords.push(units[chunk]);
  37.                 }
  38.                
  39.                 if (scaleIndex > 0) {
  40.                     chunkWords.push(scales[scaleIndex]);
  41.                 }
  42.                
  43.                 words.unshift(chunkWords.join(' '));
  44.             }
  45.             
  46.             scaleIndex++;
  47.         }
  48.         
  49.         return words.join(' ');
  50.     }

  51.     const amount1Words = numberToWords(Math.floor(amount1));
  52.     const amount2Words = numberToWords(Math.floor(amount2));
  53.    
  54.     return `${amount1Words} ${currency1} and ${amount2Words} ${currency2}`;
  55. }
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

评分

参与人数 1金币 +222 收起 理由
Grayson.Shang + 222 很给力!

查看全部评分

回复 使用道具 举报
Grayson.Shang活字格认证 Wyn认证
超级版主   /  发表于:2025-6-27 08:30:39
3#
感谢大佬支持
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部