How to quickly convert numbers into text
For example, 1 => one, 2=> two, ..., 15=》Fifteen
What I am currently thinking about is to save Chinese characters into a In array
$arr = ['一','二','三','四','五','六','七','八','九','十'];
But this is too troublesome. How to convert the input numbers into Chinese? Is there an easier way?
The final result is as follows:
Input 12
Output
一二三四五六七八九十十一十二
If you can describe the digital conversion process clearly in words, then writing code is a process of translation.
The algorithm is actually just business logic with very strange business logic. The focus is on analyzing this business logic
You can convert Arabic numerals to Chinese numerals even before you graduate from elementary school. You must have a method in your mind to complete this conversion process. So the focus of the investigation here is the abstractionprocess of the problem. To be abstract and accurate, you must first have a comprehensive analysis and understanding of the problem itself and find out the rules. In the process of digital conversion, when is 0 called ten and when is it called zero. This process is so natural in the mind that it still requires a certain amount of ability to analyze its situation and find out the rules.
I recommend this article. The analysis at the beginning of the article is the abstraction process, which is very important for programming
How to quickly convert numbers into text
For example, 1 => one, 2=> two, ..., 15=》Fifteen
What I am currently thinking about is to save Chinese characters into an array
But this is too troublesome. How to convert the input numbers into Chinese? Is there an easier way?
The final result is as follows:
Input 12
Output
The following is the implementation of
JavaScript
Observe the numerical representation of Chinese characters
Chinese characters explicitly specify the order of numbers. For example,
One hundred and one
contains the order of hundreds (one hundred and ten million)
Ten2
>>>>Two
10
>>>>Ten
16
>>>>Six
>>>>Sixteen
105
>>>>Zero ten
Five
>>>>One hundred and five
1024
>>value + order
ten.
In addition, when the following conditions are met,
need to omit the order or value10
corresponds toone ten
writing110
one hundred and ten
. For example,writing
one hundred and ten8
becomes["eight"]- ,
-
-
numbers - greater than 10
One hundred and one
101
becomes
["one", "zero", "one"]Add level
101becomes
["一", "百", "十", "zero", "一", "一"]Filter, for example,
One hundred and onebecomes the simplest form
One hundred and oneTool function
Achieved
First implement the mapping ofto the number system
and returnof Chinese characters
For example, enter
1012
4
6
8
16
32
... intotwo
four
eight
sixteen
thirty-two
...
https://github.com/wilon/php-...
PHP numbers are converted into Chinese character descriptions, and RMB is capitalized
Supports hundreds of billions of numbers. Modifying the ws array can be infinitely expanded. Modifying the cns array to Traditional Chinese can be used to capitalize RMB
Attached is the idea
/a/11...