简体   繁体   English

将大数字字符串解析为数字格式

[英]Parse large number string to number format

I have a number string which consists of: "969239274411254159183486" 我有一个数字字符串,其中包括:“ 969239274411254159183486”

I need this to format into this format (with either Javascript or Jquery): "969,239" so that it is in 'thousands'. 我需要将其格式化为以下格式(使用Javascript或Jquery):“ 969,239”,以使其成千上万。

I tried parseInt without success, what do I need to use to format it into the right format? 我尝试parseInt失败,我需要使用什么来将其格式化为正确的格式?

Thanks in advance. 提前致谢。

EDIT 编辑
Just copying the comment here posted by the person asking the question in order to avoid confusion. 只需将提问者张贴的评论复制到此处,以免造成混淆。

Input is the large string, output to be expected is 969,239. 输入为大字符串,预期输出为969239。 And yes, always the first 6. 是的,始终是第一个6。

Just take the first 6 digits, make it a number and use toLocaleString on it 只需取前6位数字,使其为数字并在其上使用toLocaleString

 const s = "969239274411254159183486"; console.log(Number(s.substr(0,6)).toLocaleString('en')); 

If you want the first six digits formatted with a comma, use slice . 如果要用逗号格式化前六个数字,请使用slice

 const str = "969239274411254159183486"; const res = str.slice(0, 3) + "," + str.slice(3, 6); console.log(res); 

 const str = "969239274411254159183486"; function getDecimal( input , numBeforeComma , numAfterComma){ const res = str.slice(0, numBeforeComma) + "," + str.slice(numBeforeComma, numAfterComma+numBeforeComma); return res; } console.log(getDecimal(str,3,3)); console.log(getDecimal(str,3,2)); 

I made a function where you can specify the amount of numbers before the comma. 我做了一个函数,您可以在逗号前指定数量。 And the amount after. 和金额之后。 It returns it for you. 它为您返回。 you can parseInt the result. 您可以解析结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM