How to get the last digit of a long? [duplicate] - java

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);

Related

How to print character repeatedly using specifier instead of loop in java? [duplicate]

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 ) ;
====================

How to round to 1 decimal place using Java [duplicate]

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.

How can I find the minimum possible difference between two floats [duplicate]

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?

how can I use padTo for padding to the left? [duplicate]

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

String split() method not giving right result [duplicate]

This question already has answers here:
Java String.split() sometimes giving blank strings
(3 answers)
Why in Java 8 split sometimes removes empty strings at start of result array?
(3 answers)
Closed 8 years ago.
I have a string like "2020". When I split it with split(""). Then i checked its length. It is giving me 5 . But it should give 4. What is the reason

Categories

Resources