简体   繁体   English

如何在javascript或jquery中向字符串添加一些数字

[英]How can I add some number to string in javascript or jquery

I have a problem. 我有个问题。 Script parsing csv to html. 脚本将csv解析为html。 But number is read as a string. 但是数字被读为字符串。 How can I add "0" to numbers that do not have to decimals. 如何将“ 0”添加到不必为小数组成的数字中。 For example: 例如:

15,45
12,00
14,2
14,54

I want to add 0 to all numbers like 4,2 我想在所有数字上加0,例如4,2

15,45
12,00
14,20
14,54

Try 尝试

  var output = "15,2".split(",").map(function(val){ return val > 100 ? val: (val+"00").slice(0,2);}).join(","); alert(output); var output = "15,100".split(",").map(function(val){ return val > 99 ? val: (val+"00").slice(0,2);}).join(","); alert(output); var output = "15,".split(",").map(function(val){ return val > 100 ? val: (val+"00").slice(0,2);}).join(","); alert(output); 

In vanillaJS vanillaJS中

var num = "14,2";

/* string to number conversion */
num = +(num.replace(',','.'));

/* set 2 digits after decimal point */
num = num.toFixed(2);

/*
Input   Output
--------------
14,25   14.25
14,2    14.20
14      14.00  */

Reduced in a single statement, as suggested in the comments below: 减少为单个语句,如下面的注释所示:

(+num.replace(',','.')).toFixed(2);

if you have an array of strings you could easily convert them using Array.map() 如果您有一个字符串数组,则可以使用Array.map()轻松地将它们转换

var nums = ["14", "14,2", "14,25"];
nums = nums.map(n => (+n.replace(',','.')).toFixed(2)); 

// => ["14.00", "14.20", "14.25"]

If you get all strings as posted format in the OP you could use length : 如果您在OP中将所有字符串作为发布格式获取,则可以使用length:

 ["15,45","12,00","14,2"].forEach(function(v){ alert(v.split(',')[1].length==1?v+"0":v); }) 

You can use this: 您可以使用此:

 var arr = ["15,45","12,00","14,2","14,54"]; var newarr = arr.map(function(o){ var val = o.split(','); return val[1].length == 1 ? val.join(',')+"0" : val.join(','); }); document.querySelector('pre').innerHTML = JSON.stringify(newarr, 0, 4); 
 <pre></pre> 

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

相关问题 如果表中的行数是 Javascript/jQuery 的某个数字,如何添加 class - How to add a class if the number of rows in a table is some number with Javascript/jQuery 如何在字符串javascript中添加条件? - How can I add condition in string javascript? 如何向 jQuery 中的某些锚元素添加 GET 参数? - How can I add a GET param to some anchor elements in jQuery? 如何使用JavaScript或jQuery将一些文本插入textarea? - How can I insert some text into a textarea using JavaScript or jQuery? 如何使用JavaScript添加一些文本发布? - How can i add some text to post by using JavaScript? 我在 reactjs 有一个秒表,如何将每个数字添加到某种数组中以显示每个数字? - I have a stopwatch in reactjs, how can I add each number into some sort of array to show each number? 如何在javascript / jquery中添加字符串美元金额? - How do i add a string dollar amount in javascript/jquery? javascript:如何将字符串数字转换为 Javascript 中的数字而不删除点后的任何数字? - javascript: How can I convert a string number to number in Javascript without remove any number after dots? 如何限制我可以用jquery动态添加的表行数 - how to limit the number of table rows I can dynamically add with jquery 如何限制div jquery或javascript的子代数? - How I can limit number child of div jquery or javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM