yajima-tatsuro / jp-yen-formatter
日本円の数値を「千円」「万円」などの単位に変換・フォーマットするPHPパッケージ
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/yajima-tatsuro/jp-yen-formatter
Requires
- php: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
README
日本円の数値を「千円」「万円」「億円」などの読みやすい単位に変換・フォーマットするPHPパッケージです。
Note for English speakers: This package is designed for Japanese native speakers, so the README is written in Japanese. If you need English documentation, please use browser translation features or other translation tools.
インストール
composer require yajima-tatsuro/jp-yen-formatter
使い方
use JpYenFormatter\Converter; // 千単位への変換 echo Converter::convert(1000, '千'); // "1千" echo Converter::convert(1500, '千'); // "1.5千" (割り切れない場合は自動で小数点第一位まで) // 万単位への変換 echo Converter::convert(10000, '万'); // "1万" echo Converter::convert(15000, '万'); // "1.5万" // 百万単位への変換 echo Converter::convert(1000000, '百万'); // "1百万" echo Converter::convert(1500000, '百万'); // "1.5百万" // 億単位への変換 echo Converter::convert(100000000, '億'); // "1億" echo Converter::convert(150000000, '億'); // "1.5億"
使用例
use JpYenFormatter\Converter; // 基本的な変換 $result = Converter::convert(5000, '千'); // 結果: "5千" $result = Converter::convert(50000, '万'); // 結果: "5万" // 割り切れない場合(デフォルトで小数点第一位まで表示) $result = Converter::convert(1500, '千'); // 結果: "1.5千" $result = Converter::convert(23000, '万'); // 結果: "2.3万" // 小数点桁数を指定 $result = Converter::convert(12340, '千', 2); // 結果: "12.34千" $result = Converter::convert(123400, '万', 3); // 結果: "12.34万" // 大きな数値の自動フォーマット(カンマ区切り) $result = Converter::convert(100000000000, '億'); // 結果: "1,000億" $result = Converter::convert(100000000, '万'); // 結果: "10,000万" // 未定義の単位を指定した場合 $result = Converter::convert(1000, '円'); // 結果: "1000円"
対応している単位
以下の単位に対応しています:
- 千: 1,000単位
- 万: 10,000単位
- 百万: 1,000,000単位
- 億: 100,000,000単位
小数点表示の動作
デフォルトでは、割り切れる場合は整数表示、割り切れない場合は小数点第一位まで自動表示されます:
// 割り切れる場合 echo Converter::convert(2000, '千'); // "2千" echo Converter::convert(30000, '万'); // "3万" // 割り切れない場合 echo Converter::convert(1500, '千'); // "1.5千" echo Converter::convert(23000, '万'); // "2.3万"
小数点桁数を手動で指定することも可能です:
// 小数点第二位まで表示 echo Converter::convert(12340, '千', 2); // "12.34千" // 小数点なし(整数のみ) echo Converter::convert(1500, '千', 0); // "2千" (四捨五入)
テスト
composer test
ライセンス
MIT License
開発者
yajima-tatsuro