Solved

padding longs

Posted on 2012-04-05
7
295 Views
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?
0
Comment
Question by:dpalyca755
  • 3
  • 3
7 Comments
 
LVL 14

Assisted Solution

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

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

Open in new window

0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 166 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);

        System.out.println(fmt_nbr);
        
        /*
        
        Output:
        
        050
        
        */
     }
}

Open in new window

0
 

Author Comment

by:dpalyca755
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Accepted Solution

by:
CPColin earned 334 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."
0
 

Author Comment

by:dpalyca755
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);        
---------------------------------------------------------------------------
Error:
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
0
 
LVL 14

Expert Comment

by:CPColin
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?
0
 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 45
print map entry 34 55
use lov values 2 36
hibernate jars 4 0
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now