数字(货币)转换成英文单词 [英] number (currency) into english words

查看:66
本文介绍了数字(货币)转换成英文单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成发票的应用程序.该系统使用三种货币:英镑、美元和欧元.作为要求的一部分,发票必须用英文显示总金额.

I have an application that generate invoices. The system works with three currencies: Pounds, US Dollars and Euros. As part of the requirements, invoices must have the total amount displayed in english words.

例如:
100.50 英镑 - 一百英镑五十便士.
100.50 美元 - 一百美元五十美分.
€100.50 - 一百欧元五十美分.

For example:
£100.50 - One hundred Pounds and fifty pences.
$100.50 - One hundred US Dollars and fifty cents.
€100.50 - One hundred Euros and fifty cents.

我发现 这篇 来自 2010 年的帖子,但它并不是真的解决我的问题.我想知道将数字转换为包括货币在内的单词的最佳方法是什么.
我正试图找到一颗宝石来帮助我解决这个问题,但我可以找到...
非常欢迎任何建议.

I found this post from 2010, but It doesn't really solve my problem. I would like to know what is the best way to convert numbers into words including the currency.
I am trying to find a gem to help me on this matter, but I could find...
Any suggestion is very welcome.

推荐答案

您链接的帖子提到了languages.gem - 因为它可以为您转换数字,您需要做的就是拆分主要和次要单位(即美元和美分/磅和便士),分别处理,然后重新组合成一个带有主要和次要单位名称的字符串,即

The post you linked mentions linguistics.gem - since it can convert the numbers for you, all you need to do is split out the major and minor units (i.e. dollars and cents/pounds and pence), process each, then recombine into a string with the major and minor unit names, i.e.

majorUnit.en.numwords dollars [and minorUnit.en.numwords cents]
...
puts 100.en.numwords + " dollars and " + 50.en.numwords + " cents"
# => 100 dollars and fifty cents

linguistics.gem 的一个好处是它也可以处理单数/当您只有一个主要/次要单元时,您可以使用复数形式,例如:

A bonus of linguistics.gem is that it can also handle singular/plural for you for when you have only one of your major/minor units, eg.:

"penny".en.plural
# => "pence"

这篇关于数字(货币)转换成英文单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆