The Artima Developer Community
Sponsored Link

Java Buzz Forum
2 Examples to Add Zeros at the Beginning of a Number in Java (Padding)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
2 Examples to Add Zeros at the Beginning of a Number in Java (Padding) Posted: Oct 13, 2014 9:17 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: 2 Examples to Add Zeros at the Beginning of a Number in Java (Padding)
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
How do you left pad an integer value with zeroes in Java when converting to a string? This is a common requirement if you are working in finance domain. There are so many legacy systems out there which expect input of certain length, and if your input is shorter than specified length, you got to add zeros at the beginning of number to make them of right length. Java has rich API and thankfully neither converting integer to String is difficult nor formatting String to add leading zeros. In fact there are multiple ways to add zeros at the start of a number or numeric String, you can either use powerful String.format() method or it's close cousin printf() method, or you can go back to DecimalFormat class, if you are still working in JDK 4. Formatting in general is a very useful concept and as Java developer you must have a good understanding of that. Earlier we have learned about formatting floating point numbers in Java and that knowledge is going to help a lot. There we have learned about using both String.format() and DecimalFormat to create floating point number up-to two, three or four decimal places, and if you have read that article then you are already familiar with tricky formatting instructions we pass to format() method e.g. "%07d", in this article we will learn how to left pad an integer value in Java by adding zeros at in front of the number.
Read more »

Read: 2 Examples to Add Zeros at the Beginning of a Number in Java (Padding)

Topic: The Heroes of Java: Dan Allen Previous Topic   Next Topic Topic: Beginner’s Guide To Hazelcast Part 1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use