i have a string object
String s = "64.5369474 British pounds"
what i want is to have a methos that can get the digits out and transfer into 2 decimal place,
the result i expect to get is something like:
String result = "64.54 British pounds"
any suggestions?
I'll guide you but will not show you a full solution. One way is to:
Split the String to extract the number - See String#split.
Convert the first String (Here I assume that the format is fixed) using Double#parseDouble.
Use DecimalFormat to truncate the number.
The result String can be easily reconstructed.
DecimalFormat df = new DecimalFormat("00.00");
String unformatedMoney = "65.432784327489";
String formattedMoney = df.formart(unformattedMoney);
System.out.println(formattedMoney + " British pounds");
Related
I do have some numbers like 100,000 and I want output as 1 Lakh in Indian numbering system. is there any method that supports it?
Detailed Example.
Input 5,50,000
Output 5.5 Lakh
You could try stripping the commas, casting to an integer, the dividing to get the number of Lakhs:
DecimalFormat df = new DecimalFormat("#.#####");
String input = "5,50,000";
double lakhs = Double.parseDouble(input.replaceAll(",", "")) / 100000;
String out = df.format(lakhs);
System.out.println(out);
Demo
You can use this github library:
https://github.com/fabiomsr/MoneyTextView
That is display amounts of money in different formats.
I was wondering if there is an existing method to convert a formatted number String to number, such as "123,456.78" to 123456.78
Basically, unlike DecimalFormat function, which turns a double variable to a String following that a given format such as "###,###.##" pattern. I want to implement a reverse of this functionality, which turns a String with "###,###.##" format to a double. Is there APIs to do this?
Thank you.
You should have looked through the documentation for DecimalFormat and its superclass. You would have discovered that it has not only format methods, but also parse methods like this one.
The easiest way to do what you want is:
NumberFormat format = NumberFormat.getInstance();
Number value = format.parse(string);
// If you specifically want a double...
double d = value.doubleValue();
You will have to catch ParseException and deal with it. How you do that depends on what you want to do when your string does not represent a valid numeric value. If it's user input, you may want to ask the user to enter the text again.
Here is a simple way to do this
String number = "20,000,000";
int x = Integer.parseInt(number.replace(",", ""));
System.out.println(x);
You just replace the char's that not belong to a number with "" and then parse it into a primitive.
String number = "20,000,000.56";
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(5);
double x = Double.parseDouble(number.replace(",", ""));
System.out.println(df.format(x));
It is a bit different for a Double cause it will display the exponential output and you'll have to prevent that. The code above does that.
df.format(x)
Returns a String but you can cast it with the Double.parseDouble method
Here's a method using a Regex and the replace method if you have more than one delimiter and you know them all :
Let's say the delimiters here are "-" and ","
double x = Double.parseDouble(number.replace("[-,]", "");
I have a String that represents an amount of money passed from input that will optionally contain a decimal point and trailing zeros. It can look like any of these:
inputA = "123.45"
inputB = "123"
inputC = "123.4"
inputD = ".50"
Is there a way to convert these so that they all have the format 0.00 with at least one digit to the left of the decimal point and exactly two to the right without having to convert to a number object like BigDecimal and then back?
You can use DecimalFormat to achieve formatting.
DecimalFormat format = new DecimalFormat("##0.00");
System.out.println(format.format(10));
Output : 10.00
Tricks:
String formattedDouble=String.format("%.2f", Double.valueOf(strDouble));
And, %.2f will format your double as 1.00, 0.20 or 5.21. Double.valueOf(strDouble) convert your String double into a double.
i want to format my double value to 2 decimals and then make it "text to speech".
this is my code:
mares = mass * acc;
DecimalFormat df = new DecimalFormat("#.00");
df.format(mares);
String mare = String.format("The force is %f", df);
home.speak(mare,TextToSpeech.QUEUE_FLUSH, null);
but it crashes, i don't know why, i put 5 and 6 and it should multiply them and give me 30.00 or something like that.
when i remove DecimalFormat the result is 30.00000000000000, i just don't like it, too many zeros.
can someone help me please?
Thanks in advance!
Your DecimalFormat is returning the formatted string, but you are ignoring it, and passing it as an argument to String.format, which certainly isn't right.
Assign the return of df.format to a string for further reference:
String mare = df.format(mares);
Or pass the numeric value directly to String.format, with the appropriate format precision specified:
String mare = String.format("The force is %.2f", mares);
I am trying to convert an input string to euro/Bulgarian currency,I am having two scenario's.
First,
When input is 10,000 the Bulgarian format should be like 10 000 and euro format should be 10.000
Second,
if the input is 10.23 then both European and Bulgarian format should be 10,23.
I am trying to do using Big Decimal,Something like,
String s = "+000000055511.00";
BigDecimal b = new BigDecimal(s.replace(",", "."));
b.setScale(2, RoundingMode.HALF_UP);
System.out.println(b.toPlainString());
But I am not able to do it as an common utility which takes and converts into euro or bulgarian currency.Is there any utility for the same?Can somebody help me?
You may use java.text.NumberFormat.getCurrencyInstance(Locale) with appropriate locals. If there are no such locals which match your requirements then construct your own decimal formatter java.text.DecimalFormat with pattern ##' '##0.00 resp. ##,##0.00 plus appropriate currency sign. The formatter can be applied to BigDecimal:
String s = "+000000027511.00";
BigDecimal B = new BigDecimal(s);
// don't replace "." by ",": english number format expected here
b.setScale(2, RoundingMode.HALF_UP);
NumberFormat f = new DecimalFormat(...); //initialize as requested see docs
System.out.println(f.format(b));
Another question is why you don't want fraction digits if your number is 10,000? If this is really the case you must define two formatters more and must select them according to your creteria.
Hope this helps.