Sum of numbers of a number [duplicate] - java

This question already has answers here:
How to sum digits of an integer in java?
(22 answers)
Closed 6 years ago.
Say I have the number 123
Is there a computationally efficient way of adding 1+2+3 and getting the answer back
Ideally I'd avoid division or parsing to String as I feel this can be quite inefficient.

No need of parsing to string.
int sum=0;
while(n>0)
{
sum += n%10; // add the last digit
n/=10; // remove the last digit.
}

Related

How to count comma in a String java [duplicate]

This question already has answers here:
How do I count the number of occurrences of a char in a String?
(48 answers)
Closed 2 years ago.
I have a String, which contains a lot of commas.
I want to count all the commas in the String but I don't know how.
I'm using (split(",")-1)
But the problem is that if I input a String like this: One,Two,Three,Four,,,
Then it returns only 3 while I want it to be 6.
I think it is because split(",") returns a String[] that does not include null or empty values.
Is there any way to solve this problem?
One straightforward way would be to just compare the length of the input against the input with all commas removed:
String input = "One,Two,Three,Four,,,";
int numCommas = input.length() - input.replace(",", "").length();
System.out.println(numCommas); // prints 6

How to take 2 digits number in string and convert it to int [duplicate]

This question already has answers here:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.

Java String.format() leading zeros and 1 decimal place [duplicate]

This question already has answers here:
Add leading zeroes to number in Java? [duplicate]
(5 answers)
Closed 4 years ago.
I need a string like 50 to appear as 050.0. I am using String.format, but I can't figure out how to do leading zeros and a single decimal place at the same time. So far, I have tried String.format("%3.2f", number);, but that isn't working as I still get 50.0 rather than 050.0
Use DecimalFormat to control the number of mandatory digits:
DecimalFormat df = new DecimalFormat("#000.0");
System.out.println(df.format(50)); // 050.0
where
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
You can use StringBuilder class to create a string with number 0 and then append it with you number and insert the decimals at the end.
int num = 50; /*Your number*/
StringBuilder s_num = new StringBuilder("0");
s_num.append(num);
s_num.append(".0");
String f_num = s_num.toString();

How to add Leading Zero with getMonth in Java (Android) [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Adding a leading zero to a large string in Java
(2 answers)
Closed 8 years ago.
I'm using an int variable:
month = dp.getMonth() + 1;
currently getting an output of "2" and when I do the following:
if (month<10){
month = '0'+month;
};
I get: 50.
Your problem is that your '0' char is being coerced to an integer. Since '0' has an ASCII value of 48, you're getting 48 + 2 = 50.
Note that what you're trying to do won't work - you can't add a leading 0 to month, as month is a number. A leading zero only makes sense in a string representation of a number.
As explained in this answer, here's how to produce a zero-padded number:
String.format("%02d", month);

How to use zero as prefix if value less than equals to 9 [duplicate]

This question already has answers here:
Left padding a String with Zeros [duplicate]
(20 answers)
Closed 8 years ago.
How to add zero as prefix if value less than equals to 9, i am using below way of achieving this:
int countZero = 0;
if(countVat <= 9)
{
countVat = countZero + countVat;
Log.d("countVat:", String.valueOf(countVat));
}
but this not works for me, still getting single digit if countVat value less than equals to 9.
Use String.format
String.format("%02d", num);

Categories