This question already has answers here:
How to convert an Int to a String of a given length with leading zeros to align?
(8 answers)
How can I pad a String in Java?
(32 answers)
Closed 6 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I have a number, I want to pad this number with zero's in the beginning if the digits are less that 9 digits.
currently if I have a number lets say:
val num = "123"
if I use padTo(9,"0") I will get "123000000" but I want "000000123"...
what is the best solution for this?
would be better to get the solution in scala
thanks
Related
This question already has answers here:
Get the decimal part from a double
(18 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
For example if I had a (double 123.987) how can take what comes after the right of the decimal and make it A integer (987)?
Well you can use String and split to derive your output:
double number = 123.456;
String numberString = String.valueOf(number);
String[] digits2 = numberString.split("\\.");
System.out.println(digits2[1]);
After having this output, you can cast it to integer. Should not be that hard
This question already has answers here:
Generate a random double in a range
(7 answers)
Using Math.round to round to one decimal place?
(9 answers)
Closed 4 years ago.
I am trying to round to the nearest decimal value, however, this line of code keeps returning a number between 0 and 1, I also want the output to be between 1 and 10. Where am I going wrong?
power[i] = rng.nextDouble();
Math.round(ThreadLocalRandom.nextDouble(1,10)*10)/10.0
You can use JDK's Math.round.
A detailed example can be found here.
This question already has answers here:
How to alter a float by its smallest increment (or close to it)?
(7 answers)
How to get the smallest next or previous possible double value supported by the architecture?
(2 answers)
Closed 5 years ago.
If I have a java float and I want to increment/decrement it by the minumim possible value how can I determine that value? I mean it is a difference if the value of the float is already a very large number like 3.4028235E38 or a very small number like 1.4E-45. In the first case to note any difference I need to subtract a far lager number as I would need to add to the second case right?
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:
Gets last digit of a number
(12 answers)
Closed 8 years ago.
I am trying to write Java code to obtain the last digit of a long (i.e. the rightmost digit).
If I were trying to obtain the last element of a string, I would use the substring() method.
What is the alternative or equivalent way when getting the last digit of a long?
You can do
long digit = Math.abs(number%10);