ocpjp


setMaximumDigits method of NumberFormat class


Please justify the output for the below code.
Code:
import java.text.NumberFormat;
import java.text.ParseException;
public class Number_format_Demo {
/**
* #param args
* #throws ParseException
*/
public static void main(String[] args) throws ParseException {
NumberFormat nf=NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
String s[]={"111.234","222.5678"};
for(String st:s)
System.out.println(nf.parse(st));
}
}
Output:
111.234
222.5678
Question:
If we have set the maximum digits as 3 then how come the second line of output is correct?
The method setMaximumFractionDigits is used with the method "format" not with the method "parse". Moreover, float datatypes can be formatted not the String datatype.
So, the correct program could be as follows.
import java.text.NumberFormat;
import java.text.ParseException;
public class Number_format_Demo {
/**
* #param args
* #throws ParseException
*/
public static void main(String[] args) throws ParseException {
NumberFormat nf=NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
Float s[]={111.234f,222.5678f};
for(Float st:s)
System.out.println(nf.format(st));
}
}

Related Links

setMaximumDigits method of NumberFormat class

Categories

HOME
logging
abc
microcontroller
latex
dynamic
hiveql
angularjs-directive
jshell
osx-yosemite
pyodbc
ejb-3.0
jpql
fetch
velocity
drush
camunda
saucelabs
minimum
google-closure-compiler
android-actionbar
alert
audio-streaming
udeploy
multipart
opengraph
google-drive-android-api
inline-assembly
workflow-foundation
launchd
ds-5
selenium3
edsdk
node-soap
imageprocessor
android-geofence
campaign-monitor
nscollectionview
php-mongodb
vpython
wufoo
intervention
logan-square
chrome-custom-tabs
hdpi
ionic2-select
django-tables2
jcs
playframework-1.x
sharpssh
fckeditor
papyrus
hotkeys
audiorecord
periodic-processing
dynamics-nav
overlapping
controlpanel
angular-promise
transport
openhab
winforms-interop
joomla3.3
web-standards
stereotype
vspackage
waterline
httpmodule
ultraedit
parseexception
beagleboard
attiny
tigase
ios-enterprise
scjp
behance-api
episerver-7
satisfiability
infor-eam
dynamic-expresso
select2-rails
qmainwindow
report-viewer2010
motordriver
android-memory
commonsware
capitalization
gmagick
lightopenid
noise-reduction
cinder
jquery-attributes
for-xml
cross-cutting-concerns

Resources

Encrypt Message