How to remove digits after decimal in a double? - android

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.

Related

Decimal point issue in displaying digits

I have to display digit in two decimal points.
Let say value 0 should display as 0.00 and value 2.3 shoule display as 2.30.
To achive this I have done something like below :
Log.e(">>> percent ", ">> " + data.percent)
percent = String.format("%.2f", percent).toDouble()
Log.e(">>> percent ", ">> " + percent)
But the result you will notice is as below :
2021-04-15 11:55:54.580 10061-15047/com.dev E/>>> percent: >> 0
2021-04-15 11:55:54.580 10061-15047/com.dev E/>>> percent: >> 0.0
It should be 0.00 instead of 0.0
What might be the issue?
Please guide. Thanks.
Double type does not have information about how to display.
So, do not use toDouble() method to the string.
Also, add 0 to the format string to denote the zero padding.
Log.e(">>> percent ", ">> " + data.percent)
val percentStr: String = String.format("%.02f", data.percent)
Log.e(">>> percent ", ">> " + percentStr)
You can use DecimalFormat https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
val df = DecimalFormat("0.00")
df.format(percent)
just change
String.format("%.2f", percent).toDouble()
to
String.format("%.02f", data.percent)

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

String to display number currency in 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.

How can I round numbers (2 decimal) parseXml get(field) in Android

TextView.setText(XMLdata.get("XMLField") + " mm");
input values that are expressed in an abbreviated way:
10,5858555966668 (with comma)
Which I would like to convert into:
10,58
if your XMLdata.get("XMLField") value is float u can do the following code
String finalValue = String.format(
"%.2f", XMLdata.get("XMLField"));
TextView.setText(finalValue + " mm");
if your XMLdata.get("XMLField") is String convert it into float do the above method.
TextView.setText((Math.floor(MathXMLdata.get("XMLField") * 100.0) / 100.0).toString() + " mm");
This converts 10,5858555966668 to 1058,58555966668, rounds it to 1058 and converts it back to 10,58.
Note: Math.floor always rounds down (cuts off all missing fractional numbers). If you intend to round to the closest value, use Math.round.
Thank you, thank you very much rmkrishna and Faro ((= I do it with
TextView.setText(String.format("%.2f",Double.parseDouble(XMLData.get("Son7GunlukSagis").replace(",", ".")))+ " mm");

Formate String after dot "." from a string in java-Android

I have a string like 46542.5435657468, but i want format this string and need only two charector after dot "." like 46542.54. Please suggest me which String method i need to use.
String.format("%.2f", Double.valueOf("46542.5435657468"));
maybe String.format()?
String.format("%.2f", floatValue);
You can use DecimalFormat
first declare this at the top
DecimalFormat dtime = new DecimalFormat("#.##"); //change .## for whatever numbers after decimal you may like.
then use it like this
dtime.format(your string);
like:
String a = "46542.5435657468";
dtime.format(a);
output will be 46542.54
You can use a method like this.
private static String extract(String text) {
String[] values = text.split(".");
return values[0] + "." + values[1].substring(0, 2);
}
The method indexOf tell you the position of the character "." ok?
The method substring cut a peace of the string from the begining (value 0) until the positio of the character "." plus 2 digits more.
public String getNumberFormated(String yourNumber)
{
return yourNumber.substring(0, yourNumber.indexOf(".") + 2);
}
Do you like my solution?

Categories

Resources