Create German currency format - android

I'm going to create a German currency format in Android, I just can't get the thousand, million, separator work. This is what I did so far:
String bill_subtotal = String.format("%.2f", bill_amount);
txtSubtotal = (TextView) findViewById(R.id.txtSubtotal);
txtSubtotal.setText(String.valueOf(bill_subtotal).replace(',', '.'));
Those above only make values like this: 60000.50 into 60000,50
I want to make it 60.000,50
Is there a way to make it like that?

My solution is ,
public static String formatGermanCurrency(double number) {
NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
return nf.format(number);
}

Related

how to read from file raw and use in String.format

I want to read data from a raw file and replace the format in the text.
For example... In a raw file like this:
hello {0}, my name id {1}, my age is {2}....
When I use String.format, as shown below, the text loses its indentation.
String data = readTextFile(this, R.raw.input);
data = String.format(data, "world", "josh", "3");
Does anyone know how to do this without losing indentation?
Code that you provided looks more like String.format e.g from C#. String.format in Java does not work this way, it's more like printf.
You can manipulate your input to looks like this.
String input = "hello %s, my name id %s, my age is %s";
String.format(input, "world", "josh", "3");
output:
hello world, my name id josh, my age is 3
indentation should be the same
EDIT
If you want to use brackets you can use MessageFormat.format instead of String.format.
String messageInput = "hello {0}, my name id {1}, my age is {2}";
MessageFormat.format(messageInput,"world", "josh", "3");
You can use Regular Explessions with pattern like that: "{/d++}":
String format (String input, String... args) {
Pattern p = Pattern.compile("{/d++}");
String[] parts = p.split(input);
StringBuilder builder = new StringBuilder("");
int limit = Math.min(args.length, parts.length);
for(int i = 0; i < limit; i++){
builder.append(parts[i]).append(args[i]);
}
return builder.toString();
}
I found the solution for my problem.
there is a needed in one more variable, it's impossible to assignment into same variable
String data = readTextFile(this, R.raw.input);
String output = String.format(data, "world", "josh", "3");

Truncate value after decimal point in android

I want to truncate the value from the given value 32.1500 into 32 and display it on textview text please help me out i searched a lot but did not found anything.
If you don't want to cast it you can keep it at double but truncate the trailing zeroes like this:
textView.setText(String.format("%.0f", 5.222));
Try to use cast like this.
double number=32.1500;
int numbereditted=(int) number;
textView.setText("Number= "+numbereditted);
Or use decimal format.
double number=32.1500;
DecimalFormat df=new DecimalFormat("0");
textView.setText("Number= "+df.format(number));
Try something like this:
private final DecimalFormat decimalFormat = new DecimalFormat("#.####");
private final DecimalFormat noDecimalFormat = new DecimalFormat("#");
String decimalValue = decimalFormat.format(value);
String noDecimalValue = noDecimalFormat.format(value);
Or using NumberFormat instead of DecimalFormat:
private NumberFormat formatter;
// Set the properties for the number format that will display the values
formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(4);
formatter.setMaximumFractionDigits(4);
String decimalValue = formatter.format(value);
formatter.setMinimumFractionDigits(0);
formatter.setMaximumFractionDigits(0);
String noDecimalValue = formatter.format(value);
You can set the rounding mode you want like this:
noDecimalFormat.setRoundingMode(RoundingMode.DOWN)
formatter.setRoundingMode(RoundingMode.DOWN)

How to show a double formatted as a price in a textview in android

I want to show in a textView a price value like: 12,199.99 in a textview in Android. but I have the value stored in a double variable (12199.99). Is there any way to show that double in the textview in the format I want?
If you want to format it using the phone's locale with 2 decimals do like this:
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
String formattedValue = decimalFormat.format(yourDoubleValue);
yourTextView.setText(formattedValue);
Hope it helps.
You can use a BigDecimal for it, for example
public static BigDecimal doubleToScale(double d, int scale){
return new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP);
}
I hope that helps.
Use String.format. More documentation on the formatting options can be found here.

Convert String to Double excluding part of the string

How to convert double to a string value that I get from the spinner, for example: "10 (ton)."
In summary:
How to convert string to double and delete the part (ton)?
I need only the value 10
Another option is using Java's substring method in the String class.
The signature is:
substring(int beginIndex, int endIndex)
Where endIndex equals to the index of the last character you want to include + 1.
In the case of your example, it will look like this:
String myString = "10 (ton)";
Double dbl = Double.parseDouble(myString.substring(0, 2));
Here is the link to the method:
Java substring
You must parse this String. Here is some example. Also use search.
Check these methods Double.parseDouble() and Double.toString() use these functions for converting double to string or vice-versa.
first you have to get rid of "(ton)" which can be achieved by using a String method for example
String inputString = "10 (ton)";
String str = inputString.split(" ")[0];
After that just parse the double Value
Double dbl = Double.parseDouble(str);
BTW: Not sure whether you want to go from double to string or vice-versa
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;

Removing space from Edit Text String

In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.

Categories

Resources