Intl.NumberFormat数字格式化应用

介绍如何使用Intl.NumberFormat对象对数值进行格式化处理,包括货币、百分比、角度、字节、面积、质量、温度、长度、容量等多种数据类型转换。

Check out Intl.NumberFormat数字格式化应用 on CurateClick

• Copy the embed code to showcase this product on your website

• Share on X to spread the word about this amazing tool

Intl.NumberFormat数字格式化应用>Web前端开发过程中常会对后端传递过来的数值类型的对象进行格式化处理,以便用户更好的阅读。例如:数据统计里的数值、货币、百分比、角度、字节、面积、质量、温度、长度、容量及一些符合单位等数据类型进行处理转换。所以下面看看关于Intl.NumberFormat对象对这些数值如何处理。三位分节法对数字进行处理,如:货币、百分比、角度、字节、面积、质量、温度、长度、容量及一些符合单位等。文章详情:http://www.deathghost.cn/article/javascript/92const formatter = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'meter',});const res = formatter.format(8848.86);// 输出:8,848.86米const formatter = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'megabyte',});const res = formatter.format(1024);// 输出:1,024 MBconst formatter = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'hour-per-week',});const res = formatter.format(40);// 中文输出:40小时/周const formatter = new Intl.NumberFormat('zh-CN', { style: 'currency', // 输出人民币 currency: 'CNY', // 输出美元,语言设置为'en' // currency: 'USD', // currencySign选项启用记帐格式 currencySign: 'accounting',});const res = formatter.format(5000000);//输出结果:¥5,000,000.00const formatter = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'gram',});const res = formatter.format(500);// 输出 500 gconst formatter = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'celsius',});const res = formatter.format(28);// 输出:28°Cconst formatter = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'degree',});const res = formatter.format(90);// 输出:90°const formatter = new Intl.NumberFormat('zh-CN', { notation: 'compact',});const res = formatter.format(1400000000);// 输出:14亿* 角度: degree* 面积: acre, hectare* 百分比: percent* 字节: bit, byte, kilobit, kilobyte, megabit, megabyte, gigabit, gigabyte, terabit, terabyte, petabyte* 时间类长度: millisecond, second, minute, hour, day, week, month, year* 长度或高度或深度: millimeter, centimeter, meter, kilometer, inch, foot, yard, mile, mile-scandinavian* 质量:gram, kilogram, ounce, pound, stone* 温度: celsius, fahrenheit* 容积: liter, milliliter, gallon, fluid-ounce

Latest Weekly Picks

make.ink favicon
Weekly Pick

Your AI tattoo generator for pro-grade concepts

Leo Wade
sellfy.com favicon
Weekly Pick

A code-free online store builder to turn views into revenue. Sell digital products, subscriptions, and merch, without fees or hassle.

Maris
www.videotoblog.ai favicon
Weekly Pick

Convert videos into awesome blog posts.

Video To Blog
www.sellerpic.ai favicon
Weekly Pick

SellerPic is the all-in-one AI design platform for e-commerce

SellerPic
fastimage.ai favicon
Weekly Pick

Fast Image AI instantly transforms your photos into stunning styles like Ghibli, Sketch, and Pixar. Effortlessly control image elements and create amazing effects with just one click.

Fast AI Team
linkedinpro.daisy.so favicon
Weekly Pick

AI-powered tool that transforms casual photos into professional LinkedIn headshots instantly. No photographer needed—just upload and download.

Gabriel
crevas.ai favicon
Weekly Pick

Crevas unifies Veo 3, Sora 2, Nano Banana and more into one intuitive canvas — so filmmakers can script, prompt, and generate cinematic stories without switching tools or losing consistency.

Spark Alpha
fotominiatur.com favicon
Weekly Pick

AI Foto Edit - Text to Image & Image Edit

foto miniatur