/    Sign up×
Community /Pin to ProfileBookmark

How to add zeros before a digit up to 10

Hi! Tell me how to add zeros in front of a digit up to 10?

This demo:
[https://codepen.io/ethereal94/pen/gOaQbxm](https://codepen.io/ethereal94/pen/gOaQbxm)

Thanks!

to post a comment
CSSHTMLJavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerMay 18.2020 — @ethereal#1618536 Very simple task.

Make use of JavaScript's padStart(length, str) function.

Eg.
``<i>
</i>"1".padStart(10, "0");<i>
</i>
`</CODE>

Or, making use of your code:
<CODE>
`<i>
</i>$('.box .number').each(function(i,elem) {
$(elem).text((i+1).toString().padStart(10, "0"));
});<i>
</i>
``
Copy linkTweet thisAlerts:
@etherealauthorMay 18.2020 — @Nachfolger#1618537

This does not work correctly

https://codepen.io/ethereal94/pen/oNjQgmL
Copy linkTweet thisAlerts:
@NachfolgerMay 18.2020 — @ethereal#1618539

Oh, I misunderstood the question. Simply change padStart(10, "0")) to padStart(2, "0")). Although, you could have done your do diligence and looked at the documentation. You would have figured it out the mistake long before I saw your response.
Copy linkTweet thisAlerts:
@HarshShahJun 11.2020 — Hii Ethereal,

Using this code you can set how many zeroes u want to enter before the digit.
``<i>
</i>
import java.util.Scanner;

class Example {
public static void main(String args[]) {

String givenNumber;
int zeroCount;

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number : ");
givenNumber = sc.next();

System.out.println("Enter total numbers of zeros : ");
zeroCount = sc.nextInt();

String formatter = "%0" + (givenNumber.length() + zeroCount) + "d";

System.out.println("Final number : " + String.format(formatter, Integer.valueOf(givenNumber)));
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@NachfolgerJun 11.2020 — @HarshShah#1619388

This is completely wrong. **Java is NOT JavaScript**. Do not conflate the two.
Copy linkTweet thisAlerts:
@mathiasJun 11.2020 — Hello !

This is the solution with your code

$('.box .number').each(function(i,elem) {<br/>
let resultat = ++i;<br/>
$(elem).text(resultat &lt;10 ? '0${resultat}' : resultat);<br/>
});


> Replace just the ' by ` I can't do it here

Have a nice day 😀
Copy linkTweet thisAlerts:
@CYPRUSBREWJun 19.2020 — @ethereal#1618539 Seems to work just fine!
×

Success!

Help @ethereal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...