开发者

String format in javascript

开发者 https://www.devze.com 2023-03-19 03:32 出处:网络
I\'d like to format a number and remove starting 0s from it. e.g.: 001 => 1 10 => 10 Actually, I have a jQuery method which does the following:

I'd like to format a number and remove starting 0s from it. e.g.:

001 => 1

10 => 10

Actually, I have a jQuery method which does the following:

  $('#myelement').text($('#myElement').text()+1);  

When the element's text is 0, the function makes it 01,开发者_运维百科 How can I make it to show 1 using jQuery or javascript?

Thanks.


what you need:

parseInt($('#myElement').text(), 10) + 1;

what you're asking for:

($('#myElement').text()+1).replace(/^0+/, '');


You can use parseInt("0010", 10).toString(). Ignore toString() if you need the int only.


Assuming you want to increment the value, convert the current text in to a number using unary +:

$('#myelement').text(function(i, v){
    return +v + 1;
});


+"0001" //1

See above! .......


$('#myelement').text(parseInt($('#myElement').text(), 10) + 1);
0

精彩评论

暂无评论...
验证码 换一张
取 消