You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JavaScript中调整数字小数位并实现加密货币价格增减?

问题描述

我正在开发一个涉及在JavaScript中处理加密货币价格的项目,需要编写一个函数,能够调整数字的小数位数,并对该数字进行指定金额的增减操作。例如:

  • 输入数字为"27055.95"时加1,结果应为"27055.96"
  • 输入"26973.9"时加1,结果应为"26974.0"

我已编写了可执行增减操作的函数,但在调整输入数字的小数位数上遇到了问题,现有代码如下:

function adjustPrice(priceString, increaseAmount) {
  const price = parseFloat(priceString);
  const newPrice = price + increaseAmount;

  // TODO: Adjust decimal points of newPrice

  return newPrice.toString();
}

console.log(adjustPrice("27055.95", 1)); // Expected output: "27055.96"
console.log(adjustPrice("27055.99", 1)); // Expected output: "27056.0"
console.log(adjustPrice("27055.00", -1)); // Expected output: "27054.99"
console.log(adjustPrice("26973.9", 1)); // Expected output: "26974.0"
console.log(adjustPrice("269", 1)); // Expected output: "270"

请问如何修改该函数,让newPrice的小数位数匹配预期输出格式?我希望函数能对价格执行增减操作后,调整结果的小数位数以匹配输入格式。比如输入格式为"27055.95",加1后输出"27055.96",减1后输出"27055.94"。是否有JavaScript内置方法或库可实现此需求?如果没有,最高效的实现方式是什么?

我还尝试使用big.js库来实现,但未能得到预期输出,尝试的代码如下:

const Big = require('big.js');

function adjustPrice(priceString, increaseAmount) {
  const bigPrice = new Big(priceString);
  const bigIncreaseAmount = new Big(increaseAmount);

  const newPrice = bigPrice.plus(bigIncreaseAmount);

  // TODO: Adjust decimal points of newPrice

  return newPrice.toString();
}

解决方案

核心思路

关键是先获取输入字符串的小数位数,再根据这个位数格式化计算后的结果。加密货币价格对精度要求高,优先推荐用big.js避免浮点数精度问题,原生JS方案需额外处理精度隐患。


方案一:使用big.js(推荐)

big.js自带小数位数格式化能力,结合输入的小数位数配置即可满足需求:

const Big = require('big.js');

function adjustPrice(priceString, increaseAmount) {
  // 提取输入的小数位数
  const decimalCount = priceString.includes('.') 
    ? priceString.split('.')[1].length 
    : 0;

  const bigPrice = new Big(priceString);
  const bigIncreaseAmount = new Big(increaseAmount);
  const newPrice = bigPrice.plus(bigIncreaseAmount);

  // 按输入的小数位数格式化结果,roundMode=1表示四舍五入(可按需调整)
  return newPrice.toFixed(decimalCount, 1);
}

// 验证测试
console.log(adjustPrice("27055.95", 1)); // "27055.96"
console.log(adjustPrice("27055.99", 1)); // "27056.0"
console.log(adjustPrice("27055.00", -1)); // "27054.99"
console.log(adjustPrice("26973.9", 1)); // "26974.0"
console.log(adjustPrice("269", 1)); // "270"

说明:

  • toFixed(d, rm)方法中,d是目标小数位数,rm为舍入模式(1为四舍五入,0为截断等),可根据业务规则选择。
  • big.js彻底解决了原生JS浮点数精度丢失问题,适合加密货币这类高精度场景。

方案二:原生JavaScript实现

若不想依赖第三方库,可通过整数转换规避浮点数精度问题,再格式化结果:

function adjustPrice(priceString, increaseAmount) {
  // 获取输入的小数位数
  const decimalCount = priceString.includes('.') 
    ? priceString.split('.')[1].length 
    : 0;
  // 计算放大倍数,将小数转为整数计算
  const multiplier = Math.pow(10, decimalCount);

  // 移除小数点转为整数,避免精度丢失
  const price = parseInt(priceString.replace('.', ''), 10);
  const adjusted = price + increaseAmount;
  // 转回小数格式
  const newPrice = adjusted / multiplier;

  // 根据小数位数格式化结果,整数情况直接转字符串
  return decimalCount === 0 
    ? newPrice.toString() 
    : newPrice.toFixed(decimalCount);
}

// 验证测试
console.log(adjustPrice("27055.95", 1)); // "27055.96"
console.log(adjustPrice("27055.99", 1)); // "27056.0"
console.log(adjustPrice("27055.00", -1)); // "27054.99"
console.log(adjustPrice("26973.9", 1)); // "26974.0"
console.log(adjustPrice("269", 1)); // "270"

说明:

  • 通过将价格转为整数计算,规避了parseFloat带来的精度误差(比如0.1+0.2≠0.3的问题)。
  • toFixed方法会自动补零,完全匹配预期格式;无小数位的输入,结果直接输出整数字符串。

内容的提问来源于stack exchange,提问作者Kvlknctk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:53:17