This question already has answers here:
Right padding with zeros in Java
(7 answers)
Closed 2 years ago.
How can I display 1 as 01 in Java when using the g.drawstring command I have tried to look but I don't know what term I should use.
You can format String and display it like this:
g.drawString(String.format("%02d", 1));
%02d is used for formatting, where 02 states for two digits with leading zero as necessary, and d - decimal number
Source: Official Oracle Documentation on formatting numeric strings
You could use Java's String.format() method, like this (recommended)
String.format("%02d", num)
Or if you could write something like this:
String text = (num < 10 ? "0" : "") + num;
If you want to print a string that contains that number, you can use String.format.
If you write something like String.format("%02d", yourNumber), for yourNumber=1 you will obtain the string 01, so you can use the previous code in a System.out or draw it on screen.
If you want to use g.drawstring, you can use the following code:
g.drawString(String.format("%02d", yourNumber), x, y)
Related
This question already has answers here:
Java output formatting for Strings
(6 answers)
Closed 11 months ago.
Given these variables:
int a = 1, b = 123, c = 55, d= 1231;
Is there a way in Java to print them with a set width of 5, say. In case number is less than five digits - only print dashes.
1----,123--,55---,1231-
I am aware that these can be achieved with some loops and if statements, looking for something similar to setw() from C++
System.out.println(String.format("%-5.5s", s).replace(" ", "-"));
In short, you can't do it directly. Java has functionality broadly similar to that of C's "printf" formatting.
You can set a field width, you can justify left or right, but your fill characters are limited to zero and space.
Documentation
If the format uses the general "%s" directive, and the corresponding argument is of a class under your control, then you can implement a 'formatTo' method to do the conversion. So a wrapper class might be useful to you.
This question already has answers here:
How can I format a String number to have commas and round?
(11 answers)
Closed 4 years ago.
Let's say I have a String variable that looks like this.
String milli = "2728462" is there a way to convert it to look something like this
2,252,251 which I guess I want as a long.
The thing is it will be passing strings in this format
1512
52
15010
1622274628
and I want it to place the , character where it needs, so if the number is 1000 then place it like so 1,000 and 100000 then 100,000 etc.
How do I properly convert a String variable like that?
Because this
String s="9990449935";
long l=Long.parseLong(s);
System.out.println(l);
Will output 9990449935 and not 9,990,449,935
Basic strategy for is to convert string representation of the number to the one of Number format. Then you have following options to represent this number according to the give Locale.
String.format()
System.out.format(Locale.US, "%,d", Long.parseLong("2728462"));
NumberFormat
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(Long.parseLong("2728462")));
You can try
String milli = "2728462"
System.out.println(NumberFormat.getInstance(Locale.US).format(Long.parseLong(milli)));
This question already has answers here:
Is there a good reason to use "printf" instead of "print" in java?
(4 answers)
Closed 9 years ago.
When is it considered good code to format a string output like this:
int count = 5;
double amount = 45.6;
System.out.printf("The count is %d and the amount is %45.6", count, amount);
utilising a printf statement, over code like this:
int count = 5;
double amount = 45.6;
System.out.print("The count is " + count + "and the amount is " + amount);
using a print statement?
I have read the JavaDocs which state that printf is "A convenience method to write a formatted string to this output stream using the specified format string and arguments."
But it doesn't state when, by convention, we should use one over the other?
So, when is it good coding to use a printf, and when is it good coding not to?
Thanks for any help.
Two methods are provided for the convenience of a coder and for different needs. If you need string formatting then go for printf method but if there is no formatting required simply use print method and void %d,%s, etc formatters.
If you want to control the precision & padding of floating point numbers then use printf otherwise go for print.
printf was added quite recently in the history of java as a convenience because so many people were used to C style printf and what it could do. so one isnt really better or preferred over the other. Regular "print" style though is clearer and simpler when special printf formatting isnt required.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Integer with leading zeroes
The program I am coding requires me to label an item with an inventory number of 012345 and store it in a int variable.
This is a stripped down example of what I am doing:
int test = 012345;
System.out.println(test);
this prints as:
5349
How do I get it to print out as 012345 rather than 5349?
EDIT: I am entering this into the parameter of a constructor for a custom class i am initializing. Then I use a method to return what the current number is, then print it to the terminal window.
You get a wrong number because when you prepend zero to an integer literal, Java interprets the number as an octal (i.e. base-8) constant. If you want to add a leading zero, use
int test = 12345;
System.out.println("0"+test);
You can also use the formated output functionality with the %06d specifier, like this:
System.out.format("%06d", num);
6 means "use six digits"; '0' means "pad with zeros if necessary".
As already said, int value with leading zero is considered as octal value. If you don't need to have test as int, why not make it string? Like
String test= new String("012345");
And if you want to use int for test, you can do not prepend 0, rather just use the number and prepend 0 at the time of printing.
In case if you're wondering how will you find how many leading zero are to be prepended, you may do like this
int lengthOfItemID=6;
int test=12345;
String test1=new String("000000"+test);
System.out.println(test1.substring(test1.length()-lengthOfItemID));
Pardon syntax mistakes, been years I last worked with java.
You can get the right result by using Integer.parseInt. That will make your string into a decimal string. (found here). The JAVA API here states that it takes a string and returns a signed decimal.
I'd like to always show a number under 100 with 2 digits (example: 03, 05, 15...)
How can I append the 0 without using a conditional to check if it's under 10?
I need to append the result to another String, so I cannot use printf.
You can use:
String.format("%02d", myNumber)
See also the javadocs
If you need to print the number you can use printf
System.out.printf("%02d", num);
You can use
String.format("%02d", num);
or
(num < 10 ? "0" : "") + num;
or
(""+(100+num)).substring(1);
You can use this:
NumberFormat formatter = new DecimalFormat("00");
String s = formatter.format(1); // ----> 01
The String class comes with the format abilities:
System.out.println(String.format("%02d", 5));
for full documentation, here is the doc
In android resources it's rather simple
<string name="smth">%1$02d</string>
I know that is late to respond, but there are a basic way to do it, with no libraries. If your number is less than 100, then:
(number/100).toFixed(2).toString().slice(2);
in less code:
print(f"{2:02} {4:03}")