Reputation: 339
I have a edittext
, and if I enter input number as 100000
, the result must be 100.000
, and if I enter input as 1000000
, the result must be 1.000.000
.
After every 3 characters from the last to beginning must have a ".
"
Here is my code:
tambah = (EditText)rootView.findViewById(R.id.total);
tambah.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
// ...
}
Upvotes: 1
Views: 190
Reputation:
here is another answer. hope it helps, simple and it would format the size for given params(formatsize, loook at the sample calls) take a look.
/** * * @param input * @param formatsize * @return */
public String format(String input, int formatsize) {
String formattedNumberString = "";
int len = input.length();
if (len < formatsize)
return input;
if (formatsize < 2) {
System.out.println("Opppps");
return "";
}
for (int i = 0; i < len; i += formatsize) {
if (i < formatsize) {
formattedNumberString = input.substring(input.length()
- formatsize - 1, input.length() - 1);
} else {
if (len - i <= formatsize) {
formattedNumberString = input + "." + formattedNumberString;
} else {
formattedNumberString = input.substring(input.length()
- formatsize - 1, input.length() - 1)
+ "." + formattedNumberString;
}
}
if (input.length() >= formatsize)
input = input.substring(0, len - i - formatsize);
}
return formattedNumberString;
}
call this function like.. (e.g.)
NumberFormatterClass nfc = new NumberFormatterClass();
System.out.println(nfc.format("1032432420000000000000", 3));
System.out.println(nfc.format("1032432420000000000000", 4));
System.out.println(nfc.format("1032432420000000000000", 5));
System.out.println(nfc.format("1032432420000000000000", 2));
regards...
Upvotes: 0
Reputation: 1504
How about doing it with following loop.
String Number=1000000;
final int THREE=3;
int noOfPoints,length,Start=0,End=0;
length=Number.length();
noOfPoints=length%THREE;
if(noOfPoints*THREE==length)
noOfPoints--;
String OutPut="";
for(int Count=0;Count<=noOfPoints;Count++)
{
Start=End;
if(Count==0)
End=length-(THREE*noOfPoints);
else
End=Start+THREE;
if(Count!=noOfPoints)
OutPut+=Number.substring(Start,End)+".";
else
OutPut+=Number.substring(Start,End);
}
Upvotes: 0
Reputation: 9700
At first format your string with %,d
format then replace all (,) with (.) as follows...
String formatedString = (String.format("%,d", 1000000)).replace(',', '.');
Log.d("Fomated String", formatedString);
Output:
D/Fomated String(20323): 1.000.000
Upvotes: 0
Reputation: 5145
Here is something I use to for dollar input. It makes sure that there are only 2 places past the decimal point at all times. You should be able to adapt it to your needs by removing the $ sign.
amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
amountEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
{
String userInput= ""+s.toString().replaceAll("[^\\d]", "");
StringBuilder cashAmountBuilder = new StringBuilder(userInput);
while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
cashAmountBuilder.deleteCharAt(0);
}
while (cashAmountBuilder.length() < 3) {
cashAmountBuilder.insert(0, '0');
}
cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
cashAmountBuilder.insert(0, '$');
amountEditText.setText(cashAmountBuilder.toString());
// keeps the cursor always to the right
Selection.setSelection(amountEditText.getText(), cashAmountBuilder.toString().length());
}
}
});
Upvotes: 1
Reputation: 4577
Use editText.setOnTextChangeListener() and check every time if the position is of multiple of 3 put a .(dot) there..
Upvotes: 0
Reputation: 4397
You should use 'java.text.NumberFormat':
java.text.NumberFormat nf = java.text.NumberFormat.getInstane();
nf.setMaximumFractionDigits(0);
String formattedNumber = nf.format(Integer.parseInt(edittext.getText());
Upvotes: 0