7个版本
0.1.7 | 2023年1月13日 |
---|---|
0.1.6 | 2022年12月17日 |
0.1.4 | 2022年11月12日 |
#131 in 值格式化
20KB
260 行
EnDinero
// convenience shorthand for formatting f32 in spanish, it will truncate the decimals we don't want
// outputs 2 decimals for amounts > 0, and 7 decimals for amounts < 1.
// This is how we need it for our crypto market pages when dealing with some crapcoins that trade in very small amounts
use endinero::dinero_f32;
assert_eq!(endinero::dinero_f32(0.1234567), "0,123 456 7");
// convenience shorthand for formatting f64 in spanish
use endinero::dinero_f64;
assert_eq!(endinero::dinero_f64(0.22233344455566), "0,222 333 444 555 66");
将浮点值转换为用户友好的数字,通常用于在西班牙语中表示货币金额(也许德语和一些其他欧洲地区的工作方式与西班牙语相同)
assert_eq!(endinero::dinero_f32(10.111), "10,11");
assert_eq!(endinero::dinero_f32(-10.111), "-10,11");
assert_eq!(endinero::dinero_f32(0.1234567), "0,123 456 7");
assert_eq!(endinero::dinero_f64(0.2223334445556677), "0,222 333 444 555 666 77");
如果您需要指定千位分隔符、小数分隔符、基数字符,对于大于0的数字或小于1的数字显示多少位小数,请使用接收所有参数的 endinero::endinero
函数。
use endinero::endinero;
assert_eq!(
endinero_f64(-1234567.456789, // amount
4, // decimals for numbers > 0
4, // decimals for numbers < 0
'.', // thousands separator
',', // radix separator
' '), // decimals separator
"-1.234.567,456 7"
);
如果您还需要为美式英语格式化美元,我们还包括了 endinero::money_f32
和 endinero::money_f64
格式化器。
// it will show only two decimals for numbers > 0
assert_eq!(money_f32(12345678.123456), "12,345,678.12");
assert_eq!(money_f32(1.123456),"1.12");
// it will show up to 7 decimals correctly for numbers < 1 if you pass an `f32` value
assert_eq!(money_f32(0.123456789),"0.123 456 7");
// we recommend you work with f64 if you're dealing with very small amounts
assert_eq!(money_f64(0.123456789),"0.123 456 789");
// up to 15 decimals correctly with f64
assert_eq!(money_f64(0.123456789012345),"0.123 456 789 012 345");
限制
整数部分在 10^14
百万亿(Cientos de billones)时使用 f64
输入时有效
assert_eq!(dinero_f64(123456789012345.1234) ,"123.456.789.012.345,12");
小数部分在 f32
输入时有效,最多7位小数
assert_eq!(endinero::dinero_f32(0.2223334445556) ,"0,222 333 4");
对于 f64,如果数字小于1,它可以格式化最多17位小数
assert_eq!(dinero_f64(0.12345678912345678) ,"0,123 456 789 123 456 78");
注意:随着整数部分的增大,小数部分可能会出现舍入问题
安装
添加到您的 Cargo.toml
endinero = "0.1.4"
许可证
版权 [2022] DiarioBitcoin.com, Angel Leon
在Apache许可证,版本2.0(“许可证”)下许可;除非您遵守许可证,否则不得使用此文件。您可以在以下位置获得许可证副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则根据许可证分发的软件按“原样”分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言、权限和限制,请参阅许可证。