Home Backend Development PHP Tutorial PHP RMB lowercase to uppercase

PHP RMB lowercase to uppercase

Jul 23, 2016 am 08:54 AM

[PHP] Code
  1. /**
  2. * Convert RMB from lowercase to uppercase
  3. *
  4. * @param string $number Value
  5. * @param string $int_unit Currency unit, default is "yuan", some requirements may be "round"
  6. * @param bool $is_round Whether to decimal Rounding
  7. * @param bool $is_extra_zero Whether to end the integer part with 0 and add 0 to the decimal number, such as 1960.30,
  8. * Some systems require the output of "One thousand nine hundred six hundred yuan zero three cents", in fact" "One thousand, nine hundred, ten yuan and three cents" is also correct
  9. * @return string
  10. */
  11. public static function num2rmb($number = 0, $int_unit = '元', $is_round = TRUE, $is_extra_zero = FALSE) {
  12. // Will Divide the number into two pieces
  13. $parts = explode('.', $number, 2);
  14. $int = isset($parts[0]) ? strval($parts[0]) : '0';
  15. $dec = isset($parts[1]) ? strval($parts[1]) : '';
  16. // If there are more than 2 digits after the decimal point, just truncate without rounding, otherwise process
  17. $dec_len = strlen($ dec);
  18. if (isset($parts[1]) && $dec_len > 2) {
  19. $dec = $is_round
  20. ? substr(strrchr(strval(round(floatval("0.".$dec), 2 )), '.'), 1)
  21. : substr($parts[1], 0, 2);
  22. }
  23. // When number is 0.001, the amount after the decimal point is 0 yuan
  24. if (empty($ int) && empty($dec)) {
  25. return 'zero';
  26. }
  27. // Definition
  28. $chs = array('0','壹','二','三','四',' Wu','Lu','旒','捌','玖');
  29. $uni = array('','十','hundred','qian');
  30. $dec_uni = array('角', 'fen');
  31. $exp = array('', '万');
  32. $res = '';
  33. // Find the integer part from right to left
  34. for ($i = strlen($int) - 1, $k = 0; $i >= 0; $k++) {
  35. $str = '';
  36. // According to Chinese reading and writing habits, every 4 characters are converted into a paragraph, and i is constantly decreasing
  37. for ($j = 0; $j < 4 && $i >= 0; $j++, $i--) {
  38. $u = $int{$i} > 0 ? $uni[$j] : ' '; //Add unit
  39. after non-0 numbers $str = $chs[$int{$i}] . $u . $str;
  40. }
  41. //echo $str."|".($k - 2 )."
    ";
  42. $str = rtrim($str, '0');// Remove the trailing 0
  43. $str = preg_replace("/0+/", "zero", $str); // Replace multiple consecutive 0's
  44. if (!isset($exp[$k])) {
  45. $exp[$k] = $exp[$k - 2] . 'Billion'; // Building unit
  46. }
  47. $u2 = $str != '' ? $exp[$k] : '';
  48. $res = $str . $u2 . $res;
  49. }
  50. // If the decimal part is 00 after processing, it is required Process
  51. $dec = rtrim($dec, '0');
  52. // Find the decimal part from left to right
  53. if (!empty($dec)) {
  54. $res .= $int_unit;
  55. // Whether to append 0 to the number ending in 0 in the integer part. Some systems have this requirement
  56. if ($is_extra_zero) {
  57. if (substr($int, -1) === '0') {
  58. $res. = 'zero';
  59. }
  60. }
  61. for ($i = 0, $cnt = strlen($dec); $i < $cnt; $i++) {
  62. $u = $dec{$i} > 0 ? $dec_uni[$i] : ''; // Add unit after non-0 number
  63. $res .= $chs[$dec{$i}] . $u;
  64. }
  65. $res = rtrim($res , '0'); // Remove the trailing 0
  66. $res = preg_replace("/0+/", "zero", $res); // Replace multiple consecutive 0's
  67. } else {
  68. $res .= $int_unit . 'whole';
  69. }
  70. return $number < 0 ? "(negative)".$res : $res;
  71. }
Copy code
[PHP] Code
  1. echo num2rmb('123.45','round');one hundred twenty three round four corners five points
Copy code
PHP


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles