format numeric currency to textual Indian number format Android - java

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.

Related

How to set Text Number Format?

i was made this method to show value of int into text view
texton= (TextView)findViewById(R.id.textcolumn);
texton.setText(String.valueOf(score));
so it will show like this
but i want to make the number text format like this, it is possible?
You can use DecimalFormat .
public class DecimalFormat extends NumberFormat
DecimalFormat is a concrete subclass of NumberFormat that formats
decimal numbers. It has a variety of features designed to make it
possible to parse and format numbers in any locale, including support
for Western, Arabic, and Indic digits. It also supports different
kinds of numbers, including integers (123), fixed-point numbers
(123.4), scientific notation (1.23E4), percentages (12%), and currency
amounts ($123). All of these can be localized.
Please have a look here
NumberFormat
How can I format a String number to have commas and round?
So Try this way ,
DecimalFormat formatter = new DecimalFormat("#,###.000");
String get_value = formatter.format(score);
texton= (TextView)findViewById(R.id.textcolumn);
texton.setText(get_value);
Logic Courtesy
You can use DecimalFormat before setting the value in textView
texton.setText(setDotsDigit(score));
public static String setDotsDigit(double value)
{
return new DecimalFormat("###.###.###").format(value);
}
You should use DecimalFormat to format your score value first.
DecimalFormat formatter = new DecimalFormat("#,###.00")`;
String s = formatter.format(score);
texton = (TextView)findViewById(R.id.textcolumn);
texton.setText(s);
Hope it will help you.

Format a number stored as a String

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.

Extract digit from string and transfer into 2 decimal

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

Currency formatting Issues

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.

Formatting in java

Good day.
I need to format a number in java.
So far I have this:
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
System.out.println(new Double(df2.format(balance)).doubleValue());
But it prints out this
110.0
121.0
133.1
146.41
161.05
But I need it to be with two digits in fraction part. How do I do it?
You don't have to get double value from formatted string.
Just use formatted string, which is returned from format() method of DecimalFormat.
So your code should be like the following:
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
...
System.out.println(df2.format(balance));
Your original code:
System.out.println(new Double(df2.format(balance)).doubleValue());
What you did in your code is: format the double value to string(which is formatted as you specified in the DecimalFormat instance). Then you convert the formatted string to Double instance and get double value from the object, which is double. And then printed it to console. So the formatted string is gone, and the double value is printed as normal.
"But I need it to be with two digits in fraction part. How do I do it?"
DecimalFormat df2 = new DecimalFormat( );
df2.setMinimumFractionDigits(2);
df2.setMaximumFractionDigits(2);
System.out.println(df2.format(balance));
You could also use the setMinimumFractionDigits method of DecimalFormat
df2.setMinimumFractionDigits(2);
your decimal format is right, but
what you are doing before you print this out is new Double(df2.format(balance)) which create new instant of double, which ignores your formatting.
so if you want to display or log your value df2.format(balance) this should be enough
ie:
System.out.println(df2.format(balance));
Try this pattern for formatting #,###,###,##.##-
DecimalFormat df2 = new DecimalFormat( "#,###,###,##.##" );
System.out.println(df2.format(balance));
This should be sufficient:
DecimalFormat df2 = new DecimalFormat("#,##0.00");
System.out.println(df2.format(balance));
The grouping for separator will follow "the interval between the last one and the end of the integer". So there is no benefit from over-specify. Example from the documentation of DecimalFormat:
The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".
Another thing is that .format() method already output a String, so there is no point in converting it to double. It will cause Exception to be thrown when balance is more than 1000 (the point when separator comes into effect, and Double class cannot parse the String with separator).

Categories

Resources