• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubts in NumberFormat

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

As i was studying chapter 6, i was studying section on NumberFormat from K&B, it is said that NumberFormat is a abstract class so get the instance
of NumberFormat we can use one of the following methods

getInstance()
getNumberInstance()
getIntegerInstance

I am confused that when i write code something like this

float f1 = 123.4678f

NumberFormat.getInstance().format(f1)
NumberFormat.getNumberInstance().format(f1)
NumberFormat.getIntegerInstance().format(f1)

All are giving the same output as
123.457
123.457
123.457

I am confused as what is difference between the above three ways
to get the instance. Please help.

Ben
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure what you are saying?
My computer is giving two different output.


float f1 = 123.4678f;
System.out.println(NumberFormat.getInstance().format(f1));//123.468
System.out.println(NumberFormat.getNumberInstance().format(f1));//123.468
System.out.println(NumberFormat.getIntegerInstance().format(f1));123



Ya here NumberFormat.getInstance() and NumberFormat.getNumberInstance() is same. No difference.

But NumberFormat.getIntegerInstance() is different, It is not going to display fractional part as it is IntegerInstance().

The returned number format is configured to round floating point numbers to the nearest integer using half-even rounding (see RoundingMode.HALF_EVEN) for formatting, and to parse only the integer part of an input string (see isParseIntegerOnly).
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply, I am sorry, that's was a slight mistake.
I got what you wanted to say, but how getInstance and getNumberInstance()
can be differciated.
Ben
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Nothing getInstance and getNumberInstance has no difference. I think there are getIntegerInstance, getCurrencyInstance etc.. with specific name like Integer, Currency, so they made NumberInstance, except name there is no difference.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks punit. That was really a helping answer.
Cheers,
Ben
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Zaidi:
Dear All,

As i was studying chapter 6, i was studying section on NumberFormat from K&B, it is said that NumberFormat is a abstract class so get the instance
of NumberFormat we can use one of the following methods

getInstance()
getNumberInstance()
getIntegerInstance

I am confused that when i write code something like this

float f1 = 123.4678f

NumberFormat.getInstance().format(f1)
NumberFormat.getNumberInstance().format(f1)
NumberFormat.getIntegerInstance().format(f1)

All are giving the same output as
123.457
123.457
123.457

I am confused as what is difference between the above three ways
to get the instance. Please help.

Ben



Ben are you sure that you were getting the same output as you have written here.

My machine gave me the following output




I have provided one more snippet of code for your better understanding that what happens when you actually write NumberFormat.getIntegerInstance().format(f1).



HTH
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JDK Source :

 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic