qzmjef 发表于 2024-11-2 19:27:17

学习PHP中国际化地数字格式处理


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不<span style="color: black;">晓得</span><span style="color: black;">大众</span>有<span style="color: black;">无</span><span style="color: black;">认识</span>过,<span style="color: black;">针对</span>数字格式<span style="color: black;">来讲</span>,西方国家会以三位为一个进位,<span style="color: black;">运用</span>逗号来分隔。<span style="color: black;">例如</span>,12345678,用标准的格式来<span style="color: black;">暗示</span>的话<span style="color: black;">便是</span> 12,345,678 。<span style="color: black;">不外</span><span style="color: black;">咱们</span>中文其实并不会有<span style="color: black;">这般</span>的分隔符,<span style="color: black;">另一</span>像某些地区则是以空格为分隔的,这个<span style="color: black;">咱们</span>马上<span style="color: black;">经过</span>代码就<span style="color: black;">能够</span>看到。其实在之前的<span style="color: black;">文案</span>中<span style="color: black;">咱们</span>就<span style="color: black;">已然</span>接触过一点这方面的知识,<a style="color: black;">学习PHP中的国际化功能来查看货币及日期信息</a>,今天就来<span style="color: black;">仔细</span>的学习一遍。至于<span style="color: black;">为何</span>要格式化数字、货币这些内容呢?<span style="color: black;">咱们</span>将在<span style="color: black;">文案</span>讲解中逐一说明。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">数字标准格式</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>还是看<span style="color: black;">咱们</span>开头介绍的标准数字格式。</p>$localeArr = [<span style="color: black;">en_US</span>, <span style="color: black;">zh_CN</span>, <span style="color: black;">ja_JP</span>, <span style="color: black;">de_DE</span>, <span style="color: black;">fr_FR</span>, <span style="color: black;">ar-IQ</span>, <span style="color: black;">ru_RU</span>];

    <span style="color: black;">foreach</span> ($localeArr <span style="color: black;">as</span> $locale) {
    $fmt = <span style="color: black;">new</span> NumberFormatter($locale, NumberFormatter::DECIMAL);
    <span style="color: black;">echo</span> $locale . <span style="color: black;">:</span>, $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL;
    }
    <span style="color: black;">// en_US:1,234,567.891</span>
    <span style="color: black;">// zh_CN:1,234,567.891</span>
    <span style="color: black;">// ja_JP:1,234,567.891</span>
    <span style="color: black;">// de_DE:1.234.567,891</span>
    <span style="color: black;">// fr_FR:1 234 567,891</span>
    <span style="color: black;">// ar-IQ:١٬٢٣٤٬٥٦٧٫٨٩١</span>
    <span style="color: black;">// ru_RU:1&nbsp;234&nbsp;567,891</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>先指定了许多的国家地区编码,<span style="color: black;">而后</span>循环它们,<span style="color: black;">运用</span> NumberFormatter 对象来对<span style="color: black;">她们</span>进行实例化。第二个参数<span style="color: black;">便是</span>要实例化的格式类型,<span style="color: black;">这儿</span><span style="color: black;">咱们</span>指定的是数字类型。<span style="color: black;">而后</span><span style="color: black;">运用</span> format() <span style="color: black;">办法</span>就<span style="color: black;">能够</span>对指定的数字进行格式化地输出了。<span style="color: black;">能够</span>看到,德国是<span style="color: black;">运用</span> . 来分隔进位,<span style="color: black;">运用</span>逗号来做为小数点。而法国和俄罗斯则是<span style="color: black;">运用</span>空格来<span style="color: black;">暗示</span>进位,逗号<span style="color: black;">暗示</span>小数点。其它国家则是沿用标准的英式<span style="color: black;">暗示</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span><span style="color: black;">非常多</span>财务及银行项目<span style="color: black;">来讲</span>,标准数字格式非常有用。<span style="color: black;">常常</span><span style="color: black;">咱们</span>接触到比较多的是在汇款时要填写的普通数字、中文大写,而<span style="color: black;">有些</span>面向企业和涉外的<span style="color: black;">机构</span>财务<span style="color: black;">亦</span>需要这种标准格式的数字来进行存根的记录。既然说到财务了,<span style="color: black;">咱们</span>再<span style="color: black;">瞧瞧</span>货币格式的展示。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">货币格式</h2><span style="color: black;">foreach</span> ($localeArr <span style="color: black;">as</span> $locale) {
    $fmt = <span style="color: black;">new</span> NumberFormatter($locale, NumberFormatter::CURRENCY);
    <span style="color: black;">echo</span> $locale . <span style="color: black;">:</span>, $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL;
    <span style="color: black;">echo</span> $locale . <span style="color: black;">:</span>, $fmt-&gt;formatCurrency(<span style="color: black;">1234567.891234567890000</span>, <span style="color: black;">RUR</span>), PHP_EOL;
    }
    <span style="color: black;">// en_US:$1,234,567.89</span>
    <span style="color: black;">// en_US:RUR&nbsp;1,234,567.89</span>
    <span style="color: black;">// zh_CN:¥1,234,567.89</span>
    <span style="color: black;">// zh_CN:RUR&nbsp;1,234,567.89</span>
    <span style="color: black;">// ja_JP:¥1,234,568</span>
    <span style="color: black;">// ja_JP:RUR&nbsp;1,234,567.89</span>
    <span style="color: black;">// de_DE:1.234.567,89&nbsp;€</span>
    <span style="color: black;">// de_DE:1.234.567,89&nbsp;RUR</span>
    <span style="color: black;">// fr_FR:1 234 567,89&nbsp;€</span>
    <span style="color: black;">// fr_FR:1 234 567,89&nbsp;RUR</span>
    <span style="color: black;">// ar-IQ:١٬٢٣٤٬٥٦٨&nbsp;د.ع.‏</span>
    <span style="color: black;">// ar-IQ:١٬٢٣٤٬٥٦٧٫٨٩&nbsp;RUR</span>
    <span style="color: black;">// ru_RU:1&nbsp;234&nbsp;567,89&nbsp;₽</span>
    <span style="color: black;">// ru_RU:1&nbsp;234&nbsp;567,89&nbsp;р.</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在这段代码中,<span style="color: black;">咱们</span><span style="color: black;">运用</span>了两种模式的输出。<span style="color: black;">第1</span>个<span style="color: black;">指的是</span>定 NumberFormatter 的第二个参数为 CURRENCY ,<span style="color: black;">亦</span><span style="color: black;">便是</span>指定格式化为货币格式。其实<span style="color: black;">便是</span>为标准格式的数字前后<span style="color: black;">增多</span>了对应地区的代币符号。<span style="color: black;">例如</span><span style="color: black;">咱们</span>中国和日本通用的 ¥ ,<span style="color: black;">通常</span>是放在金额的前面,而欧洲的则<span style="color: black;">运用</span> € 欧元标识放在金额的后面。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">另一种形式<span style="color: black;">便是</span> formatCurrency() 这个<span style="color: black;">办法</span><span style="color: black;">能够</span>指定一个货币类型,<span style="color: black;">倘若</span>不是这个类型的区域设置的话,就直接输出这个货币字符。在测试代码中,<span style="color: black;">咱们</span>给定的是俄罗斯的老卢布,其它区域中会直接输出 RUR ,而在区域设置为俄罗斯时,输出的<span style="color: black;">便是</span>标准的老卢布符号(<span style="color: black;">此刻</span><span style="color: black;">运用</span>的是新卢布,符号是 ₽ ,老卢布<span style="color: black;">便是</span> р.)。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">仔细</span>的地区格式化样式</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">是不是感觉<span style="color: black;">已然</span>很<span style="color: black;">高挑</span>上了?不不不,上面两种格式只是开胃菜,真正好玩的<span style="color: black;">此刻</span>马上端给你。</p>$fmt = <span style="color: black;">new</span> NumberFormatter(<span style="color: black;">zh_CN</span>, NumberFormatter::PERCENT);
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 123&nbsp;456&nbsp;789&nbsp;%</span>

    $fmt = <span style="color: black;">new</span> NumberFormatter(<span style="color: black;">zh_CN</span>, NumberFormatter::SCIENTIFIC);
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL;<span style="color: black;">// 1,2345678912345679E6</span>

    $fmt = <span style="color: black;">new</span> NumberFormatter(<span style="color: black;">zh_CN</span>, NumberFormatter::SPELLOUT);
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 一百二十三万四千五百六十七点八九一二三四五六七九</span>

    $fmt = <span style="color: black;">new</span> NumberFormatter(<span style="color: black;">zh_CN</span>, NumberFormatter::SPELLOUT);
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234502.891234567890000</span>), PHP_EOL; <span style="color: black;">// 一百二十三万四千五百〇二点八九一二三四五六七九</span>

    $fmt = <span style="color: black;">new</span> NumberFormatter(<span style="color: black;">zh_CN</span>, NumberFormatter::ORDINAL);
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 第1,234,568</span>

    $fmt = <span style="color: black;">new</span> NumberFormatter(<span style="color: black;">zh_CN</span>, NumberFormatter::DURATION);
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 1,234,568</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PERCENT 不多说了,百分比,<span style="color: black;">便是</span><span style="color: black;">增多</span>了一个百分号,<span style="color: black;">况且</span>不是以标准格式输出的,会以空格进行进位分隔。SCIENTIFIC <span style="color: black;">便是</span><span style="color: black;">咱们</span><span style="color: black;">平常</span>的科学计数法,测试代码中的结果<span style="color: black;">便是</span> 1.xx 的 10 的 6 次方的意思。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SPELLOUT 就比较厉害了,按当前区域语言的拼写规则。没错,直接转换<span style="color: black;">成为了</span><span style="color: black;">咱们</span>的中文<span style="color: black;">暗示</span>。<span style="color: black;">倘若</span>需要再转换成中文的大写,直接字符替换就<span style="color: black;">能够</span>了,这个绝对是这次<span style="color: black;">文案</span>的重大<span style="color: black;">发掘</span>。之前在一家<span style="color: black;">机构</span>面试的时候就有人问过<span style="color: black;">怎样</span>将数字转换成中文<span style="color: black;">暗示</span>,<span style="color: black;">由于</span><span style="color: black;">非常多</span>的财务系统都需要<span style="color: black;">这般</span>的功能。不管是做帐还是处理发票,中文大写或小写都是系统自动输出的。当时还写了半天算法,<span style="color: black;">倘若</span><span style="color: black;">大众</span>自己写算法的时候除了需要<span style="color: black;">重视</span>单位外,零的<span style="color: black;">暗示</span><span style="color: black;">亦</span>是非常重要的一点,有兴趣的<span style="color: black;">伴侣</span><span style="color: black;">能够</span>自己尝试一下。<span style="color: black;">不外</span>下回<span style="color: black;">倘若</span>面试的时候有人问这个问题,那我直接就会甩出 NumberFormatter::SPELLOUT 这个神器了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ORDINAL 是排序的<span style="color: black;">暗示</span>,在中文中其实<span style="color: black;">便是</span>在前面<span style="color: black;">增多</span>了一个 第 字。DURATION 是基于<span style="color: black;">连续</span>时间规则的格式。这两种都会抛弃掉小数点。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">格式化规则设置</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">虽然说</span><span style="color: black;">已然</span>有这么多的规则格式供<span style="color: black;">咱们</span><span style="color: black;">运用</span>了,但<span style="color: black;">大众</span>的业务总是千奇百怪的,<span style="color: black;">咱们</span>能不能定义自己的格式规则呢?既然这么写了,那当然是<span style="color: black;">能够</span>的啦。</p>var_dump($fmt-&gt;getPattern()); <span style="color: black;">// string(8) "#,##0.##"</span>
    $fmt-&gt;setPattern(<span style="color: black;">"#0.# <span style="color: black;">公斤</span>"</span>);
    var_dump($fmt-&gt;getPattern()); <span style="color: black;">// string(6) "0.# <span style="color: black;">公斤</span>"</span>
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 1234567.9 <span style="color: black;">公斤</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">看出来了吗?<span style="color: black;">咱们</span><span style="color: black;">运用</span> setPattern() <span style="color: black;">办法</span>来定义了一个带 <span style="color: black;">公斤</span> 的格式规则,很<span style="color: black;">表示</span>,<span style="color: black;">咱们</span>是需要一个<span style="color: black;">暗示</span>重量的格式。<span style="color: black;">而后</span>仅<span style="color: black;">保存</span>一位小数点,不需要分隔符号。<span style="color: black;">这般</span>再次<span style="color: black;">运用</span> format() <span style="color: black;">办法</span>的时候就会<span style="color: black;">根据</span><span style="color: black;">咱们</span>指定的格式来进行格式化了。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">属性操作</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然,除了直接设置规则格式外,<span style="color: black;">咱们</span>还<span style="color: black;">能够</span>指定<span style="color: black;">有些</span>属性值来改变当前的格式效果。</p>$fmt = <span style="color: black;">new</span> NumberFormatter( <span style="color: black;">zh_CN</span>, NumberFormatter::DECIMAL );<span style="color: black;">echo</span> <span style="color: black;">"Digits: "</span>.$fmt-&gt;getAttribute(NumberFormatter::MAX_FRACTION_DIGITS), PHP_EOL; <span style="color: black;">// Digits: 3</span>
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 1,234,567.891</span>

    $fmt-&gt;setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, <span style="color: black;">2</span>);
    <span style="color: black;">echo</span> <span style="color: black;">"Digits: "</span>.$fmt-&gt;getAttribute(NumberFormatter::MAX_FRACTION_DIGITS), PHP_EOL;<span style="color: black;">// Digits: 2</span>
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 1,234,567.89</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这段代码中,<span style="color: black;">咱们</span><span style="color: black;">经过</span> setAttribute() 来设置 MAX_FRACTION_DIGITS 的值,用于改变最大<span style="color: black;">保存</span>的小数点位数。当然,不仅限于这一个属性,还有<span style="color: black;">非常多</span>别的<span style="color: black;">能够</span>修改的属性,<span style="color: black;">大众</span><span style="color: black;">能够</span><span style="color: black;">自动</span>查阅官方手册。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">分隔符号设置</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一样</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span>直接修改格式化中的分隔符、小数点等<span style="color: black;">运用</span>的符号。直接<span style="color: black;">运用</span> setSymbol() <span style="color: black;">办法</span>就<span style="color: black;">能够</span>。</p>var_dump($fmt-&gt;getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL));<span style="color: black;">// string(1) ","</span>
    $fmt-&gt;setSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, <span style="color: black;">"*"</span>);
    var_dump($fmt-&gt;getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL));<span style="color: black;">// string(1) "*"</span>
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 1*234*567.891</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">与地区格式化<span style="color: black;">相关</span>的文本属性设置</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>还<span style="color: black;">能够</span>直接设置与地区格式化<span style="color: black;">关联</span>的<span style="color: black;">有些</span>文本信息,<span style="color: black;">例如</span>下面代码中<span style="color: black;">运用</span> setTextAttribute() 修改了负号的<span style="color: black;">暗示</span>。<span style="color: black;">咱们</span>还<span style="color: black;">能够</span><span style="color: black;">运用</span>这个<span style="color: black;">办法</span>修改间隔字符,货币编码等内容,<span style="color: black;">大众</span><span style="color: black;">能够</span>自己对照官方文档测试学习。</p>var_dump($fmt-&gt;getTextAttribute(NumberFormatter::NEGATIVE_PREFIX)); <span style="color: black;">// string(1) "-"</span>
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">-1234567.891234567890000</span>), PHP_EOL;
    $fmt-&gt;setTextAttribute(NumberFormatter::NEGATIVE_PREFIX, <span style="color: black;">"负号 "</span>);
    var_dump($fmt-&gt;getTextAttribute(NumberFormatter::NEGATIVE_PREFIX));<span style="color: black;">// string(7) "负号 "</span>
    <span style="color: black;">echo</span> $fmt-&gt;format(<span style="color: black;">-1234567.891234567890000</span>), PHP_EOL; <span style="color: black;">// 负号 1,234,567.891</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">获取地区信息</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这两个<span style="color: black;">办法</span><span style="color: black;">便是</span>简单地获取当前的地区信息了,之前在其它的<span style="color: black;">文案</span>中<span style="color: black;">咱们</span><span style="color: black;">亦</span>讲过,VALID_LOCALE 是<span style="color: black;">暗示</span>有效区域,ACTUAL_LOCALE <span style="color: black;">暗示</span>的是<span style="color: black;">实质</span>区域。</p>var_dump($fmt-&gt;getLocale(Locale::VALID_LOCALE)); <span style="color: black;">// string(10) "zh_Hans_CN"</span>var_dump($fmt-&gt;getLocale(Locale::ACTUAL_LOCALE));<span style="color: black;">// string(10) "zh_Hans_CN"</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">字符转换为数字、货币格式</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>能够将数字进行格式化地输出,输出之后的内容<span style="color: black;">由于</span><span style="color: black;">增多</span>了分隔符之类的内容,<span style="color: black;">因此</span>都会转成字符串,<span style="color: black;">那样</span>,<span style="color: black;">咱们</span>能<span style="color: black;">不可</span>把<span style="color: black;">已然</span>格式化过的标准数字字符再转回数字类型呢?</p>$fmt = <span style="color: black;">new</span> NumberFormatter( <span style="color: black;">zh_CN</span>, NumberFormatter::DECIMAL );
    $num = <span style="color: black;">"1,234,567.891"</span>;
    <span style="color: black;">echo</span> $fmt-&gt;parse($num).<span style="color: black;">"\n"</span>; <span style="color: black;">// 1234567.891</span>
    <span style="color: black;">echo</span>$fmt-&gt;parse($num, NumberFormatter::TYPE_INT32).<span style="color: black;">"\n"</span>; <span style="color: black;">// 1234567</span>


    $fmt = <span style="color: black;">new</span> NumberFormatter( <span style="color: black;">zh_CN</span>, NumberFormatter::CURRENCY );
    <span style="color: black;">echo</span> $fmt-&gt;parseCurrency(<span style="color: black;">¥1,234,567.89</span>, $currency), PHP_EOL; <span style="color: black;">// 1234567.89</span>
    var_dump($currency); <span style="color: black;">// string(3) "CNY"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">两个<span style="color: black;">办法</span>,<span style="color: black;">第1</span>个是 parse() <span style="color: black;">办法</span>,将标准格式的数字字符串转回指定类型的数字,<span style="color: black;">能够</span>指定为 TYPE_INT32 、TYPE_INT64 、TYPE_DOUBLE 、TYPE_CURRENCY 等类型。<span style="color: black;">另一</span>一个<span style="color: black;">办法</span>是 parseCurrency() <span style="color: black;">办法</span>,从名字就<span style="color: black;">能够</span>看出,它是将货币格式转回数字,并且,很重要的一点是,它的第二个引用参数,<span style="color: black;">能够</span>将货币符号的通用编码<span style="color: black;">亦</span>返回回来,<span style="color: black;">例如</span>测试代码中返回的 CNY <span style="color: black;">表率</span>的<span style="color: black;">便是</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>的人民币。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">错误信息</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后<span style="color: black;">咱们</span>来<span style="color: black;">瞧瞧</span> NumberFormatter 中的错误信息<span style="color: black;">怎样</span>获取。</p><span style="color: black;">echo</span> $fmt-&gt;parseCurrency(<span style="color: black;">1,234,567.89</span>, $currency), PHP_EOL;
    var_dump($fmt-&gt;getErrorCode()); <span style="color: black;">// int(9)</span>var_dump(intl_is_failure($fmt-&gt;getErrorCode()));<span style="color: black;">// bool(true)</span>
    var_dump($fmt-&gt;getErrorMessage()); <span style="color: black;">// string(36) "Number parsing failed: U_PARSE_ERROR"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">这儿</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>非标准的货币字符串来<span style="color: black;">运用</span> parseCurrency() 进行转换,parseCurrency() 必须接收的是带货币符号的内容,<span style="color: black;">因此</span><span style="color: black;">这儿</span>就产生了错误。<span style="color: black;">咱们</span><span style="color: black;">运用</span> getErrorCode() <span style="color: black;">能够</span>获取到错误码,<span style="color: black;">运用</span> getErrorMessage() <span style="color: black;">能够</span>获取到错误信息。<span style="color: black;">另一</span>是一个 intl_is_failure() 函数,用于<span style="color: black;">按照</span>错误码判断<span style="color: black;">是不是</span>产生了区域语言问题的错误。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">总结</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">又是大开眼界的一次学习旅程,中文小写格式的转换真的是之前完全不<span style="color: black;">晓得</span>的,而货币的互相转换我觉得<span style="color: black;">亦</span>完全<span style="color: black;">能够</span>应用到<span style="color: black;">有些</span>采集程序中,<span style="color: black;">例如</span>电商页面价格的采集分析。总之,还是感觉到收获满满的。<span style="color: black;">另一</span>,这一套 NumberFormatter 对象<span style="color: black;">亦</span>是<span style="color: black;">供给</span>了面向过程的函数式<span style="color: black;">运用</span><span style="color: black;">办法</span>的,<span style="color: black;">例如</span> numfmt_create() ,记住是 numfmt_ 开头的函数哦,不要和 number_format() <span style="color: black;">关联</span>的函数搞混了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">测试代码:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">https://github.com/zhangyue0503/dev-blog/blob/master/php/20201</span></a>1/source/4.学习PHP中国际化地数字格式处理.php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参考文档:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">https://www.php.net/manual/zh/class.numberformatter.php</a></p>




1fy07h 发表于 2024-11-7 20:47:01

你的努力一定会被看见,相信自己,加油。

wrjc1hod 发表于 2024-11-11 10:21:27

谷歌网站排名优化 http://www.fok120.com/
页: [1]
查看完整版本: 学习PHP中国际化地数字格式处理