javascript - NodeJS 如何用 . 和零?
问题描述
假设我们有数字 300,我希望将其填充为 300.000
或者数字 23,5 类似于 23.500
解决方案
希望对您有所帮助,详情见评论。
function formating(n) {
// Convert number into a String
const str = n.toString();
// Find the position of the dot.
const dotPosition = str.search(/\./);
// Return string with the pad.
if (dotPosition === -1) {
return str += ".000";
} else {
const [part1, part2] = str.split('.');
return part1 + '.' + part2.padEnd(3, "0");
}
}
推荐阅读
- flutter - 可以在调试模式下构建应用程序,但不能在发布模式下 Flutter
- javascript - 一个php页面上的多个表单不起作用
- sql - 如何在 Spark-Sql 中以 'MM/dd/yyyy HH:mm:ss.SSS' 格式解析微秒
- html - Angular - 将父 div 的原点中心更改为左下角
- javascript - vaadin 14 lit 模板动画错误中的纸质菜单按钮
- c# - 将 Crystal Report 打印到 Intermec 打印机
- pyspark - 过滤数组类型列 Pyspark 中的日期
- xamarin.forms - Azure Pipelines Xamarin.Forms iOS 构建失败并出现错误 MSB4057:项目中不存在目标“_IsProjectRestoreSupported”
- python - Tensorflow 多 GPU 训练 (A100),不稳定的利用率。如何解决?
- node.js - 我正在尝试将 React 本机 tcp 客户端连接到节点服务器,但未建立连接