android


Android Currency - java.lang.NumberFormatException: For input string: “099 ”


java.lang.NumberFormatException: For input string: "099 "
This is the error I'm getting when I run this code from another SO answer that formats an EditText into currency (two numbers after comma).
I will repost my code here:
price.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
String current = Double.toString(sub.getPrice());
#Override
public void afterTextChanged(Editable s) {
if(!s.toString().equals("")){
price.removeTextChangedListener(this);
String replaceable = String.format("[%s,.]", NumberFormat.getCurrencyInstance(locale).getCurrency().getSymbol());
String cleanString = s.toString().replaceAll(replaceable, "");
double parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formatted;
price.setText(formatted);
price.setSelection(formatted.length());
price.addTextChangedListener(this);
}
}
});
price is simply an EditText.
What I tried so far:
I tried forcing the currency to be USD ($) instead of being based on locale and the error doesn't show up, While if I use EURO (based on locale) I get the error.
Also what I noticed that switching the currency from USD to EUR the symbol changes from something like $9,00 to 9,00 €. I suspect that the space between the numbers and the € causes the NumberFormatException but I have no clue on how to fix it.
The space in "9,00 €" is indeed causing the problem.
Now you have two options:
Add the whitespace to the replaceable characters to remove it, i.e. we include \s:
String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance(locale).getCurrency().getSymbol());
Or trim the space before trying to parse it as a double:
cleanString = cleanString.trim();
double parsed = Double.parseDouble(cleanString);

Related Links

Clicking an Item in a listview, to bring up a new view
SharedPreferences not updating
Android VideoView and ImageView rotate image
Toggle GPS Programmatically Android 4.4
Managing notifications
Launching Google Play Store Intent not working
How to make AlertDialog.builder's negative button invisible?
Android database location in app directory
How to extract the Video Muxer part from StagefrightRecorder?
Delphi XE5 Firemonkey TStringGrid OnClick event
Can i parse JSON on the main thread if I have the file locally
How to get version of android (4.1.1, 4.4.2,…) in Delphi
Animation set. cant figure out how to make two animations with one image and one animationset
How to program an external monitor app for MAC with android
how can show alert dialog in another activity?
How to share in-app-purchases between two Android apps

Categories

HOME
url-redirection
checkstyle
angular2-material
rsa
apiblueprint
vxml
onclick
python-multithreading
minecraft-forge
octave
commonjs
plist
resultset
mxgraph
stellar.js
renjin
firefox-addon
connector
libigl
dataflow-diagram
postscript
bulletphysics
shared
nstimer
angular-translate
calculation
autodesk-designautomation
send
mustache
fiware-cygnus
openerp-7
sessionstorage
metawidget
attributeerror
softmax
memory-mapping
facebook-social-plugins
double-click
magento-2.0.7
globalize
remedy
arq
scalding
qtranslate
oozie-coordinator
java-websocket
reachability
redbeard-ios
flex4.6
windows-azure-queues
gogs
clipboard.js
jtwig
spreadjs
sklearn-pandas
nio2
ascii-art
directdraw
vigenere
jdi
rubaxa-sortable
haskell-warp
google-refine
matlab-deployment
zend-currency
android-broadcast
apache-spark-1.3
nodelist
webmatrix-3
machine-instruction
device-tree
wcm
mobility
reflector
ray
jquery-slider
mutual-authentication
model-driven-development
jeromq
pclzip
windows-98
mysql-error-2013
boost-msm
os.system
correlated-subquery
cross-database
htdocs
android-holo-everywhere
collectionviewsource
diagrams
uideviceorientation
ie-compatibility-mode
object-database
murmurhash
product-management
lemmatization

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile