padding longs

Posted on 2012-04-05
Medium Priority
Last Modified: 2012-04-05
I am doing some math using longs which will yield numbers like 5, 10, 20, etc
but I always need to pad the result to be 3 digits.
What's the best way to do that?
Question by:dpalyca755
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 14

Assisted Solution

CPColin earned 1336 total points
ID: 37814127
Using zeros in a DecimalFormat will work:

new DecimalFormat("000").format(x);

Open in new window

LVL 23

Assisted Solution

wdosanjos earned 664 total points
ID: 37814135
Use the DecimalFormat class.  Here is an example:
import java.text.*;

public class Test
    public static void main(String args[])
        DecimalFormat formatter = new DecimalFormat("000");
        long nbr = 50;

        String fmt_nbr = formatter.format(nbr);


Open in new window


Author Comment

ID: 37814190
Can I use the String Format method to do so?
What is the syntax if do so?
let's say the 5 I referenced earlier is stored in the variable myString.
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 14

Accepted Solution

CPColin earned 1336 total points
ID: 37814211
Assuming you have a long, it would be something like this:

String.format("%03d", myLong);

Open in new window

The "0" means "pad with zeros," the "3" means "make the output three characters wide," and the "d" means "output a number."

Author Comment

ID: 37814296
I can't compile that for some reason.
I tried that exact line too.
What am I doing wrong?

Code Snippet:
long c = 5;
String pad2 = String.format("%03d", c);        
test.java:51: cannot resolve symbol
symbol  : method format (java.lang.String,long)
location: class java.lang.String
             String pad2 = String.format("%03d", c);
1 error
LVL 14

Expert Comment

ID: 37814319
I've only seen that happen when using a version of Java older than 1.5 or when I've told the compiler to pretend like its from an old version of Java. Do you know if that's happening?

Author Comment

ID: 37814330
Yup....I was accidentally pointing to an older version.
I fixed it.  Works now.

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question