I want to print a double No. in TextView like this format 10699657.6, currently this is like 1.06996576E7 . please can any one help me?
DecimalFormat dform = new DecimalFormat("0.###E0");
Similar question found here:
Convert scientific notation to decimal notation
There is DecimalFormat class, which does exactly what you want.
Related
I am trying to convert the English numbers (1, 2, 3) to Bengali numbers (১, ২, ৩).
For example, if I get 10000, then I want to show like ১০,০০০.
I can replace the number one by one with the Bengali counterpart using replaceAll method
But I want to know if there is an alternative solution to do that instead of the above.
Use this:
val convertedString = String.format(Locale.forLanguageTag("bn"), "%d", 1234567890)
I have used NumberFormat from popular library Intl and converted it easily like below
NumberFormat("##,##,##,###", "bn").format(10000)
And the output is:
১০,০০০
I am coding a maths app and I want to show special characters such as PI, E, or subscripts and all those things.
I want to show them on the xml file of the layout.
How can I do it?
Thank you guys for all!
You can use the Unicode value for the symbol, preceded by \u. For example, the pi character is "\u03C0"
This site: http://www.dionysia.org/html/entities/symbols.html has list of elements which can be used in xml. Just watch the second element. For example:
square = √
THen you need to conver it. For example:
String symbol = Html.fromHtml(square);
Alternative link is here: http://www.hrupin.com/2011/12/how-to-put-some-special-math-symbols-in-textview-editview-or-other-android-ui-element
The characters in a string resource are unicode. You can include special characters using the \unnnn notation.
There are many places to look up the unicode values on the web. Google found this one for me:
http://inamidst.com/stuff/unidata/
I have a question, I am doing an App that is a kind of bill however I don't know how to express in a textview for example:
1120 as $1.120 or something like this.
Thanks in advance
numberformat is your solution :
Test code :
int = 1236;
NumberFormat nf = NumberFormat.getInstance(Locale.US);
Log.i("test", nf.format(nb));
show output :
1,236
There are a number of solutions to format numbers in Android which of course depend on the nature of your app and your requirements.
Because you are using a money value in your example, I would look into Big Decimal at has numerous precision and rounding methods.
Here's a little example for you, note that it is not localised!
BigDecimal theAmount = new BigDecimal(int);
DecimalFormat decimalFormat = new DecimalFormat("$#,##0.00;-$#,##0.00");
// ^ This is set manually, you could use a localised format to have Android set the
// values based on locale like this: DecimalFormat.getInstance();
decimalFormat.setDecimalSeparatorAlwaysShown(true);
decimalFormat.setMinimumFractionDigits(3);
decimalFormat.setMaximumFractionDigits(3);
// ^ There are a lot of methods to check out here!
TextView yourTextView.setText(decimalFormat.format(theAmount.divide(new BigDecimal(1000))));
Good luck!
I am writing code in Adobe Flash Builder for an Android application. I have written my code to do some math and return the answer to a label field. I would like to know how do I return this answer to show only 1 spot after the decimal. Here is the code
lblAnswer.text = String(Number ((sldrABSL.value) + 46.7)/28.7);
If there are any suggestions please let me know.
If I understand this correctly, you have a string representing a number, which you want to be presented with only one decimal.
Fist of all, you'd have to convert the numeric value of the string to a double:
String stringOfNumber = "100.1233123";
Double number = Double.valueOf(stringOfNumber);
Secondly, you'd have to establish the format of which to represent the double (number of decimals):
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
Set the digit to a (i.e) TextView:
myTextView.setText("" + oneDigit.format(number));
I think this should work. Is this kind of what you were asking?
Edit: Not super certain as to how to set it to a textview, but in java, printing it to screen works like this:
System.out.println(oneDigit.format(number));
Edit2: Oh, and you'll need to this import:
import java.text.DecimalFormat;
Edit3:
TextView.setText("" + oneDigit.format(number));
works fine for me.
The decimal places uses the tofixed property. It must be added at the end of the specified number that needs to contain the decimal place.
'lblAnswer.text = String(Number ((sldrABSL.value) + 46.7)/28.7).toFixed(1);
The one specifies the number of decimal places that are used.
I want to have a parameter in one string in strings.xml and this parameter should be a double value. So I use %1$f. Here - http://developer.android.com/reference/java/util/Formatter.html there are many examples, but what if I want to have have a few double/float parameters and I want only the second one to have 2 digits after .? I tried to use combinations like %2$.2f or %2.2$f. Nor of them worked. %.1f does not work as well.
So, does anybody know how can I "customize" a float/double value inside a strings.xml? Thanks.
Just adding to #David Airam's answer here; the "incorrect" solution he gives is actually correct, but with a bit of tweaking. The XML file should contain:
<string name="resource1">Hello string: %1$s, and hello float: %2$.2f.</string>
Now in the Java code:
String svalue = "test";
float sfloat= 3.1415926;
String sresult = getString(R.string.resource1, svalue, sfloat);
The exception that #David Airam reported is from trying to jam a String into a format specifier with %f, which requires a floating point type. Use float and there is no such exception.
Also, you can use Float.valueOf() to convert a String to a float in case your input data was originally a string (say, from a EditText or something). However, you should always try/catch valueOf() operations and handle the NumberFormatException case, since this exception is unchecked.
%.1f work for me if you like to show only 1 digit after ','
Define is strings.xml file
<string name="price_format">$%,.2f</string>
//For using in databinding where amount is double type
android:text="#{#string/price_format(model.amount)}"
//For using in java runtime where priceOfModifier is double type
amountEt.setText(context.getResources().getString(R.string.price_format, priceOfModifier));
This worked for me.
<string name="market_price">Range ₹%1$.0f - ₹%2$.0f</string>
android:text="#{#string/market_price(viewModel.suggestedPriceRange.max, viewModel.suggestedPriceRange.min)}"
Outputs: Range ₹500 - ₹1000
In ₹%1$.0f, .0f defines how many digits you want after the decimal.
A simpler approach:
<string name="decimalunit">%.2f%n</string>
float sfloat= 3.1475926;
String sresult = getString(R.string.decimalunit, sfloat);
Output: 3.15
If it were me I'd store the values in the resources as simple values, and then use formatter methods to control how they're displayed, roughly like this
public String formatFigureTwoPlaces(float value) {
DecimalFormat myFormatter = new DecimalFormat("##0.00");
return myFormatter.format(value);
}
public String formatFigureOnePlace(float value) {
DecimalFormat myFormatter = new DecimalFormat("##0.0");
return myFormatter.format(value);
}
I now that this reply is arriving too late... but I hope to be able to help other people:
Android sucks with multiple parameters substitutions when you want decimal numbers and format this in common style %a.bf
The best solution I have found (and only for these kind of resources) is put the decimal parameters as strings %n$s and in the code apply my conversion with String.format(...)
Example:
INCORRECT WAY:
// In xml file:
<string name="resource1">You has a desviation of %1$s and that is a %2$.2f%% percentage.</string>
// And in java file
String sresult = getString(R.string.resource1, svalue, spercentage); // <-- exception!
This solution is technically correct but incorrect due to android substitution resources system so the last line will generate an exception.
CORRECT WAY / SOLUTION:
Simply convert the second parameter into a String.
<string name="resource1">You has a desviation of %1$s and that is a %2$s percentage.</string>
And now in the code:
...
// This is the auxiliar line added to solve the problem
String spercentage = String.format("%.2f%%",percentage);
// This is the common code where we use the last variable.
String sresult = getString(R.string.resource1, svalue, spercentage);