数字、日期与金额格式规范
格式化工具
输入一个值,每一行是一种格式,四种语言并排。
选一种值并输入。表格按英文、简体中文、西班牙文和日文显示这个值的每种格式,格式名下面是这种格式的规则。
| 格式 | English | 简体中文 | Español | 日本語 |
|---|---|---|---|---|
| 分组最多两位小数,去掉末尾的 0 | ||||
| 固定小数金额和需要对比的列保留小数位 | ||||
| 缩写地方紧张时用,悬停看完整数值 |
成列的数字
等宽数字,右对齐。涨跌靠箭头表达。
相对时间和时区
最近的用文字,久一点的写日期。时区重要时写出时区。
- English
- 简体中文
- Español
- 日本語
发布会议
你的时间
- 新加坡
- 东京
- 马德里
- 纽约
脱敏
三个级别,在服务端打码。
- 姓名
- 欧**娜
- 手机
- 138****5678
- 邮箱
- j***@mail.com
- 银行卡
- •••• 4242
- 证件号
- 11************1234
浏览器拿不到被隐藏的部分。查看完整信息要再次验证,并记录日志。
空值和加载
未知写“—”,加载时放骨架条,真正的 0 才写 0。
收入
订单
转化率
收入—订单—转化率—
收入NaN订单undefined转化率$0.00
格式化模块
一个没有依赖的模块,每份方案都带着它:specs/format.js。
// LayQ format: every value on screen goes through here (specs/design.md, "Data formats"). Only Intl.
// A missing or unreadable value gives EMPTY, never NaN or undefined.
export const EMPTY = '—'
const missing = (v) => v == null || v === '' || (typeof v === 'number' && !Number.isFinite(v))
const toDate = (v) => (missing(v) || isNaN(new Date(v)) ? null : new Date(v))
const num = (v, locale, o) => (missing(v) ? EMPTY : new Intl.NumberFormat(locale, o).format(v))
const dt = (v, locale, o) => (toDate(v) ? new Intl.DateTimeFormat(locale, o).format(toDate(v)) : EMPTY)
const cycle = (hour12) => (hour12 ? 'h12' : 'h23')
export const number = (v, locale, { decimals = 2, fixed } = {}) => num(v, locale, { maximumFractionDigits: decimals, minimumFractionDigits: fixed ? decimals : 0 })
export const compact = (v, locale) => num(v, locale, { notation: 'compact', maximumFractionDigits: 1 })
export const percent = (ratio, locale) => num(ratio, locale, { style: 'percent', maximumFractionDigits: 1 })
// `direction` picks the colour, which depends on the market.
export function change(ratio, locale) {
if (missing(ratio)) return { text: EMPTY, direction: 'none' }
const step = Math.round(ratio * 1000)
const direction = step > 0 ? 'up' : step < 0 ? 'down' : 'flat'
return { text: { up: '▲ ', down: '▼ ', flat: '' }[direction] + num(ratio, locale, { style: 'percent', maximumFractionDigits: 1, signDisplay: 'exceptZero' }), direction }
}
// `minor`: in cents. `display`: 'symbol' or 'code'.
export function money(amount, currency, locale, { minor, display = 'symbol', compact } = {}) {
if (missing(amount) || !currency) return EMPTY
const digits = new Intl.NumberFormat('en', { style: 'currency', currency }).resolvedOptions().maximumFractionDigits
const o = { style: 'currency', currency, currencyDisplay: display, ...(compact && { notation: 'compact', maximumFractionDigits: 1 }) }
return num(minor ? amount / 10 ** digits : amount, locale, o)
}
// style: 'short' in tables, 'medium', 'long' in documents.
export const date = (v, locale, { style = 'medium', timeZone } = {}) => dt(v, locale, { dateStyle: style, timeZone })
export const time = (v, locale, { hour12, timeZone, zone } = {}) => dt(v, locale, { hour: '2-digit', minute: '2-digit', hourCycle: cycle(hour12), timeZone, timeZoneName: zone ? 'short' : undefined })
export function dateTime(v, locale, { style = 'medium', hour12, timeZone, zone } = {}) {
const text = dt(v, locale, { dateStyle: style, timeStyle: 'short', hourCycle: cycle(hour12), timeZone })
return zone && toDate(v) ? text + ' ' + new Intl.DateTimeFormat(locale, { timeZoneName: 'short', timeZone }).formatToParts(toDate(v)).at(-1).value : text
}
export const dateRange = (a, b, locale, { style = 'medium', timeZone } = {}) =>
toDate(a) && toDate(b) ? new Intl.DateTimeFormat(locale, { dateStyle: style, timeZone }).formatRange(toDate(a), toDate(b)) : EMPTY
// [below this age in seconds, unit, seconds per unit]; past a week, the date.
export const relativeBands = [
[60, 'now'],
[3600, 'minute', 60],
[86400, 'hour', 3600],
[604800, 'day', 86400],
]
export function relative(v, locale, { now = Date.now() } = {}) {
if (!toDate(v)) return EMPTY
const s = (toDate(v) - now) / 1000
const [, unit, size] = relativeBands.find(([below]) => Math.abs(s) < below) ?? []
const words = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' })
return !unit ? date(v, locale) : size ? words.format(Math.trunc(s / size), unit) : words.format(0, 'second')
}
// 1h 5m; style 'clock' (1:05:09) for media and timers.
export function duration(seconds, locale, { style } = {}) {
if (missing(seconds)) return EMPTY
const all = Math.max(0, Math.round(seconds))
const [h, m, s] = [Math.floor(all / 3600), Math.floor(all / 60) % 60, all % 60]
const two = (n) => String(n).padStart(2, '0')
if (style === 'clock') return (h ? h + ':' + two(m) : m) + ':' + two(s)
const unit = (n, u) => num(n, locale, { style: 'unit', unit: u, unitDisplay: 'narrow' })
return (h ? [unit(h, 'hour'), m && unit(m, 'minute')] : m ? [unit(m, 'minute'), s && unit(s, 'second')] : [unit(s, 'second')]).filter(Boolean).join(' ')
}
export function bytes(v, locale) {
if (missing(v) || v < 0) return EMPTY
if (v < 1000) return number(v, locale) + ' B'
const step = Math.min(4, Math.floor(Math.log10(v) / 3))
const n = v / 1000 ** step
return num(n, locale, { style: 'unit', unit: [, 'kilobyte', 'megabyte', 'gigabyte', 'terabyte'][step], maximumFractionDigits: n < 10 ? 1 : 0 })
}