String to display number currency in Android - android

I have a string
String retail = c.getString(c.getColumnIndex("retail"));
The date is being passed as "99999", I need it to print out as "999.99", how can I do this?

If you always have to add the "." 2 character before the end, this should work:
retail = retail.substring(0, retail.length()-2) + "." + retail.substring(retail.length()-2,retail.length());
This will add a dot two character before the end of the String, as you need.

Related

How to remove digits after decimal in a double?

my double = 42.12323532 I want it to show just "42". How do I do that? This is what I have tried-
TV_BMR.setText("BMR: " + String.format("%.2f", BMR) + " cal");
but this only rounds it to 2 digits. I want everything removed after the decimal. Including the decimal.
String str = “BMR: “ + (int)BMR;
String str = “BMR: “ + (long)BMR;
Then we can, In the high double type range numbers everything removed after the decimal.

String with \n in single string android

I have multiple value in single string, but I want to send in every string value on new line. I have written the following code, but it's not working.
String text ="Address" + strpropertyAddress +"\n"+ "Price" + strPrice;
This is my service method where i pass the string.
sendPropertyApi(text, sendto);
I have checked every where, and it is exactly same. Basically the string data should be sent in on a new line, but it's sent in on a single line.
Assuming(Because of Android tag) you are setting this text in textView or editText, for that I think you have to escape \. So use \\n instead. Try following:
String text ="Address" + strpropertyAddress +"\\n"+ "Price" + strPrice;
Or it is always better to use default line separator like following:
String text ="Address" + strpropertyAddress +System.getProperty("line.separator")+ "Price" + strPrice;

how to remove special character from string except + in android?

I am fetching number from contact book and sending it to server. i get number like this (+91)942 80-60 135 but i want result like this +9428060135.+ must be first character of string number.
Given your example you want to replace the prefix with a single + character. You also want to remove other non-numeric characters from the number string. Here's how you can do that:
String number = "(+91)942 80-60 135";
number = "+" + number.replaceAll("\\(\\+\\d+\\)|[^\\d]", "");
The regex matches any prefix (left paren followed by a + followed by one or more digits, followed by a right paren) or any non digit character, and removes them. This is concatenated to a leading + as required. This code will also handle + characters within the number string, e.g. +9428060135+++ and +(+91)9428060135+++.
If you simply wanted to remove any character that is not a digit nor a +, the code would be:
String number = "(+91)942 80-60 135";
number = number.replaceAll("[^\\d+]", "");
but be aware that this will retain the digits in the prefix, which is not the same as your example.
You can use String.replace(oldChar, newChar). Use the code below
String phone = "(+91)942 80-60 135"; // fetched string
String trimmedPhone = phone.replace("(","").replace(")","").replace("-","").trim();
I hope it will work for you.
check this. Pass your string to this function or use as per code goes
String inputString = "(+91)942 80-60 135";
public void removeSpecialCharacter(String inputString) {
String replaced = inputString.replaceAll("[(\\-)]", "");
String finalString = replaced.replaceAll(" ", "");
Log.e("String Output", " " + replaced + " " + second);
}

what is the characters behind the characters down the line?

I am converting between "\n" to ":" in file .txt. And here, this is my paragraph before convert:
You can see that, between string "Hoa" and string "Đàm", it have one character " " and two character "\n". And this is my convert function:
private String convertData(){
String different = "~`!##$%^&*()-_=+*/\\\"'|]}{[:;?/.><,\n ";
StringBuilder data = new StringBuilder(tvData.getText().toString().trim());
for (int j = 0; j < data.length(); j++){
if(different.contains("" + data.charAt(j))) data.setCharAt(j, ':');
}
String convertData = data.toString().trim();
return convertData;
}
And this is result:
You can see behind character "\n" have a character, and it is not in string different.
Can anyone tell me what to do?
1 - Proceed like you're currently doing, but the \n (and the other escaped sequences).
So, use
String different = "~`!##$%^&*()-_=+*/'|]}{[:;?/.><, ";
2 - After the loop, use string.replace() to replace only the substring "\n" and the other sequences, i.e.: "\\" and "\"".
You could use a loop to replace the sequences, but you must replace 2 characters instead of one.
Basically, in the first loop you replace all the single characters.
Then, you replace the character sequences.
Notepad only supports Windows line endings (\r\n), so that must be what your file contains. Convert the file to Unix line endings (\n) using literally any program that is not Notepad, or add \r to your search pattern.

How to get substrings from a string in android?

Hello I have a long string and its having html tags like "" and "" and ,I want to get values from this strings,can anyone give me solution how to get values from it?
my string is:
<strong>1 king bed</strong><br /> <b>Entertainment</b> - Wired Internet access and cable channels <br /><b>Food & Drink</b> - Refrigerator, minibar, and coffee/tea maker<br /><b>Bathroom</b> - Shower/tub combination, bathrobes, and slippers<br /><b>Practical</b> - Sofa bed, dining area, and sitting area<br />
my try
int start = description_long.indexOf("Food");
int end = description_long.indexOf("<br />");
String subString = description_long.substring(start,
end);
System.out
.println("===============MY SUB STRING FROM STRING============="
+ start
+ ""
+ "============end======="
+ end + "");
i want to get values of Food & Drink and Bathroom,can any one please tell me how to get these values in a seperate string in android programatically.
Better you could use a regular expression or try to parse.
<a[^>]*>([^<]*)<[^>]*>(.*)
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
Try this but values must end with <br />
String key = "<b>Bathroom</b>"; //<b>Food & Drink</b>
int start = htmlInput.lastIndexOf(key);
String value = htmlInput.substring(start + key.length(), htmlInput.indexOf("<br />", start));
System.out.println(value);

Categories

Resources