This question already has answers here:
Converting Integer to String with comma for thousands
(15 answers)
Closed 9 years ago.
Is there a simple way to format variables on a GUI with spaces (or commas) between so its easier to read.
Example:
int x = 12000000;
JLabel.setText(x);
which outputs 1200000, however I am trying to achieve on of the following.
1 200 000
1,200,000
If you want to use the user's locale to determine whether to use commas or decimal points, use the following:
String formattedNum = NumberFormat.getIntegerInstance().format(x);
For Americans, you would see 1,200,000.
You can try this:
int x = 12000000;
JLabel.setText(NumberFormat.getInstance().format(x));
Related
This question already has answers here:
Simple way to repeat a string
(32 answers)
Closed 6 months ago.
I want to print a char '=' repeatedly where I give the no. of times the asterisk should be repeated.
Example: count = 20 and I want to print ==================== using printf() and format specifiers.
String#repeat
Simply append a string generated by String#repeat method, in Java 11+.
String result = "=".repeat( 20 ) ;
====================
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I want to take a float that can be entered by the user to any acceptable number of decimal places and format it so that it is only two decimal places. I am able to do this by converting it to a String but I want to keep it as a float and was wondering if there was an easier way to do this than changing from float to string and then back to a float?
Here's a way to do it:
https://stackoverflow.com/a/153785/4119650
It's a little more complicated than the String conversion method, but it gives you greater control over the truncation.
This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 7 years ago.
The title says it all. Right now if I input a number like 100.50, in my program it prints as 100.5. Is there an easy way to make the program recognize the zero?
You can do this trick.
String s = String.format("%.2f", 100.50);
This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 7 years ago.
when ever I put a big or small number through System.out.println() (in Java), it decides to convert something like 0.003897 to 3.897E-3 which is hard to read when going through my temporary-testing-debug-log-thing.
is there any way to keep it as 0.003897?
Use DecimalFormat like: #.#######
Usage:
DecimalFormat df = new DecimalFormat("#.00");
Either use:
System.out.println( String.format( "%.6f", 123.456789d )
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In Java, can I define an integer constant in binary format?
In python, you can do something like:
a = 0b00000010 which would set a to 2.
Is it possible to do something like that in Java? I know I could just go through and assign my varibles by the number instead of binary, but I like the visual.
Thanks ~Aedon
In Java 7, you can do
int a = 0b00000010;
However if you're working with an older version, I'm afraid you're stuck with
int a = Integer.parseInt("00000010", 2);