在版本4中使用SI前缀更改数字格式 [英] formatting a number with SI prefix change in version 4

查看:99
本文介绍了在版本4中使用SI前缀更改数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第4版之前

var formatter = d3.format("s");  
    formatter(400000) // 400k

在版本4

var formatter = d3.format("s");  
    formatter(400000) // 400.000k

有什么方法可以不使用精度就获得像以前版本一样的格式.

Is there any way I can get the format like previous versions without using precision.

推荐答案

有什么方法可以不使用精度就获得像以前版本一样的格式吗?

Is there any way I can get the format like previous versions without using precision?

在不使用精度的情况下,在D3 v4.x中将不再可能.

Without using precision, this is not possible anymore in D3 v4.x.

根据文档:

根据类型,精度要么表示小数点后的位数(类型f和%),要么表示有效位数(类型e,g,r,s和p)./p>

Depending on the type, the precision either indicates the number of digits that follow the decimal point (types f and %), or the number of significant digits (types ​, e, g, r, s and p).

意思是,对于("s"),精度表示有效位数.

Meaning that, for ("s"), the precision indicates the number of significant digits.

这是有趣的部分,它在D3 v3.x API中不存在:

And here comes the interesting part, that doesn't exist in D3 v3.x API:

如果未指定精度,则除以外的所有类型默认为 6 (无),默认为12.

If the precision is not specified, it defaults to 6 for all types except (none), which defaults to 12. (emphasis mine)

因此,formatter(400000)的精度默认为6,这将为您提供:

So, the precision for formatter(400000) defaults to 6, which gives you:

400.000k

例如,如果您执行formatter(40),则会得到:

For instance, if you do formatter(40), you'll get:

40.0000

PS :在包含小数点的数字中尾随零是有效的.

PS: Trailing zeros in a number containing a decimal point are significant.

这篇关于在版本4中使用SI前缀更改数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆