Let's say I have a time hh:mm (eg. 11:22) and I want to use a string tokenizer to split. However, after it's split I am able to get for example: 11 and next line 22. But how do I assign 11 to a variable name "hour" and another variable name "min"?
Also another question. How do I round up a number? Even if it's 2.1 I want it to round up to 3?
Have a look at Split a string using String.split()
Spmething like
String s[] = "11:22".split(":");;
String s1 = s[0];
String s2 = s[1];
And ceil for rounding up
Find ceiling value of a number using Math.ceil
Rounding a number up isn't too hard. First you need to determine whether it's a whole number or not, by comparing it cast as both an int and a double. If they don't match, the number is not whole, so you can add 1 to the int value to round it up.
// num is type double, but will work with floats too
if ((int)num != (double)num) {
int roundedNum = (int)num + 1;
}
Related
I have a number as
Double d = 100.000000;
I want to remove the decimal point and print the values as 100000000
(Note I am using java)
It is impossible. double doesn't store zeroes after decimal point so 1.0000 is equal to 1.0.
Hint: you can use BigDecimal for this. It have scale.
I'm afraid 100.000000 does not equal 100000000 and as mentioned by #talex, double doesn't store the zeros after the decimal point.
Your best bet is to use a String and remove the . manually:
String s = "100.000000";
System.out.println(s.replaceAll("\\.", "")); //note '.' needs to be escaped
Output:
100000000
You could parse it as a Double then if necessary.
Format the value using String.format and the remove the separator.
double d = 100.000;
String formatted = String.format(
Locale.US, //Using a Locale US to be sure the decimal separator is a "."
"%5f", //A decimal value with 5decimal
d) //The value to format
.replace(".", ""); //remove the separator
System.out.println(formatted);
100000000
Other examples :
100.000123456 > 100000123
You can see that the value is truncated, it is important to understand that.
Note that I have set the String to have 5 decimal number, but this up to you.
the double does not store the number as 100.0000 it just stored as 100.0 that means any unnecessary zeros on the right will be deleted but if the number was like this 100.01234 u can use this trick
Double d = 100.01245;
String text = Double.toString(d);
text.replace(".","");
d = Double.parseDouble(text);
or u can store the number as sting from the beginning
String text = "100.000000";
d.replace(".","");
double d = Double.parseDouble(text);
I have some issue like this
in my textview Rs. 99.99
String val = textview.getText().toString();
Result :: val :: Rs.99.99
i am converting that into float using this way
float value = Float.parseFloat(val);
i am getting NumberFormatException: Rs.99.99 cannot convert
any one guide me
You can do the following before converting it to float
String substring = str.length() > 2 ? str.substring(str.length() - 3) : str;
You can try this way.
System.out.println(Float.parseFloat("Rs.99.99".substring(3)));
Note: You need to make sure the string always contain "Rs." in the beginning.
i am getting numberFormat Exception Rs.99.99 cannot convert
Yes, because in method
Float.parseFloat(String s);
You get
NumberFormatException -- if the string does not contain a parsable float.
And In your case it isn't,
So best option is to apply Validation to enter only floating point numbers inside text View.
It's not entirely clear what the problem is (do all the strings that cause problems begin with Rs., or are users putting other kinds of garbage at the beginning of the input)? Here's a way to remove all characters from the string, up to (but not including) the first digit:
val = val.replaceFirst("^[^0-9]*", "");
This finds the first occurrence of a pattern that starts at the beginning of the string (the first ^) and consists of 0 or more occurrences of nondigits ([^0-9]).
Hello Im new to programming
and as title I was wonder how you can find last digit of any given number?
for example when entering in 5.51123123 it will display 3
All I know is I should use charAt
Should I use while loop?
thanks in advance
You would want to do something like this:
double number = 5.51123123;
String numString = number + "";
System.out.println(numString.charAt(numString.length()-1));
When you do the number + "", Java "coerces" the type of number from a double to a string and allows you to perform string functions on it.
The numString.length()-1 is because numString.length() returns the count of all the characters in the string BUT charAt() indexes into the string and its indexing begins at 0, so you need to do the -1 or you'll get a StringIndexOutOfBoundsException.
You can use below function simply and you can customize data types accordingly,
private int getLastDigit(double val){
String tmp = String.valueOf(val);
return Integer.parseInt(tmp.substring(tmp.length()-1));
}
You can't use charAt on a floating point variable (or any other data type other than Strings). (Unless you define a charAt method for your own classes...)
You can, however, convert that floating point number to a string and check the charAt(str.length()-1).
Convert your float variable to string and use charAt(strVar.length - 1).
double a=5.51123123;
String strVar=String.valueOf(a);
System.out.print(strVar.charAt(strVar.length -1);
Double doubleNo = 5.51123123;
String stringNo = doubleNo.toString();
System.out.println(stringNo.charAt(stringNo.length()-1));
value
double value = 345.12345;
String Format 1
String str = String.format("%.02f", value);
String Format 2
String str = String.format("%.2f", value);
Both print the same value 345.12. So, what is the difference between these two?
The 0 prefix notation only works for the integer part, not the decimal part. If you had say %07.2f instead, it would show the value as 0345.12.
The syntax for a format specifier is
%[argument_index$][flags][width][.precision]conversion
Note that precision is 02 in the first case and 2 in the second case, but since these are the same number, the output is the same. Hence "%.02f" and "%.2f" are the same.
But, if you had written %02f, then flags would be 0 ("zero-pad output up to width") and width would be 2. Then the output would be zero-padded up to the specified width.
I'd like to get the integer value from my string. Below is my example.
String strScore = "Your score is 10. Probability in the next 2 years is 40%";
But I just want to get the score which is 10. How can I do this?
UPDATED:
String firstNumber = strScore.replaceFirst(".*?(\\d+).*", "$1");
bfLog.createEntry( firstNumber );
I save this to sqlite database.
You can use one of the String regex replace methods to capture the first digits in a captured group:
String firstNumber = strScore.replaceFirst(".*?(\\d+).*", "$1");
.*? consumes initial non-digits(non-greedy)
(\\d+) Get the one or more available digits in a group!
.* Everything else (greedy).
This depends on whether anything else can change in your string.
If it's always the same apart from the number, you can use
int score = Integer.parseInt(strScore.substring(14,16))
because the digits "10" are at index 14 and 15 of the string.
If other stuff changes in your string, you should use a regular expression :
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
You can try this:
String strScore = "Your score is 10. Probability in the next 2 years is 40%";
String intIndex = strScore.valueOf(10);
String intIndex = 10 // Result