尝试从右侧添加字符串,从右侧移到特定区域

发布于 2025-02-08 13:35:11 字数 933 浏览 0 评论 0原文

我一直在尝试弄清楚如何在某个地方添加一个字符串,这是代码

function fixBalance(amount){
  if(amount <= 99999999){
    var amt = "0." + amount.toString().padStart(8, "0");
    return Number(amt);
  } else {
    return false;
  }
}

console.log(fixBalance(1));
console.log(fixBalance(1000));
console.log(fixBalance(10000000));
console.log(fixBalance(1000000000));

基本上,通过8个数字后,有8个空间在0的右侧

,我希望数字不断超过该期间

1 = 0.00000001

1000 = 0.00001000或0.00001

10000000 = 0.10000000 or 0.1

100001000 = 0.100001000

1000000001 = 10.00000001

1010000000 = 10.10000000

10100000000 = 101.00000000

如果我要放入1000000000,则应是10.00000000或10作为数值数字,

试图弄清楚如何使用其他语句来做这件事,

谢谢!

I've been trying to figure out how to add period as a string in a certain place, here's the code

function fixBalance(amount){
  if(amount <= 99999999){
    var amt = "0." + amount.toString().padStart(8, "0");
    return Number(amt);
  } else {
    return false;
  }
}

console.log(fixBalance(1));
console.log(fixBalance(1000));
console.log(fixBalance(10000000));
console.log(fixBalance(1000000000));

Basically, there are 8 spaces to the right of the 0

After it passes 8 numbers, I want numbers to keep going past the period

For example:

1 = 0.00000001

1000 = 0.00001000 or 0.00001

10000000 = 0.10000000 or 0.1

100001000 = 0.100001000

1000000001 = 10.00000001

1010000000 = 10.10000000

10100000000 = 101.00000000

If I were to put in 1000000000, it should be 10.00000000 or 10 as a numerical number

Trying to figure out how to do that with the else statement

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤浅笑 2025-02-15 13:35:14

在组成之前,如何单独确定整数和十进制部分?

function fixBalance(amount){
  const divisor = 100_000_000;
  const integer = Math.floor(amount / divisor);
  const decimal = amount % divisor;
  
  return integer.toString() + "." + decimal.toString().padStart(8, "0");
}

console.log(fixBalance(1));
console.log(fixBalance(1000));
console.log(fixBalance(10000000));
console.log(fixBalance(1000000000));

How about determining the integer and decimal parts separately before composing them?

function fixBalance(amount){
  const divisor = 100_000_000;
  const integer = Math.floor(amount / divisor);
  const decimal = amount % divisor;
  
  return integer.toString() + "." + decimal.toString().padStart(8, "0");
}

console.log(fixBalance(1));
console.log(fixBalance(1000));
console.log(fixBalance(10000000));
console.log(fixBalance(1000000000));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文