• 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

Confused with answers from (K&B) Chapter 6: Question 13

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

i need help.

I've tested the following example either in eclipse and with console (with javac and java).



According to the answers from K&B the output should look like this:

Answer D.
987.12346
987.123456

------------------------------
My output in eclipse:
987,12346
987123456

My output in javac/java:
987,12346
987123456
is exactly the same.


Now my confusion.
* Why does my output looks so different (the colon instead uf a dot and look at the parsed string s)? I'm shure i have not made any typing errors (at least in the code).
* Why is the output of this: 987,12346
i'd expected something like this: 987,12345
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.



HALF_EVEN Rounding Mode

2.



I'm guessing that NumberFormat.setMaximumFractionDigits can only be applied to numbers. It explicitly says, "Numbers" in the API.
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm guessing that NumberFormat.setMaximumFractionDigits can only be applied to numbers. It explicitly says, "Numbers" in the API.



But why schould work an strings, too?

My output contains no colon(,) or dot(.), but it seems that (from K&B) there should be at least a dot (because there is actually one in the string).

I think, that i will flop questions about NumberFormat and stringparsing, and i dont want to.
 
Ryan Beckett
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No comma's nor decimal symbols because its not a String. parse() returns java.lang.Number
A java.lang.Number is an abstract representation of a number. It's not floating point.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sebastian tortschanoff wrote:
Now my confusion.
* Why does my output looks so different (the colon instead uf a dot and look at the parsed string s)? I'm shure i have not made any typing errors (at least in the code).
* Why is the output of this: 987,12346
i'd expected something like this: 987,12345



Hi sebastian,
the above s.o.p statement will print 987.12346 because in the fractional part the 5th digit is rounding to 6.
as per basic mathematical rule
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi sebastian,
the above s.o.p statement will print 987.12346 because in the fractional part the 5th digit is rounding to 6.
as per basic mathematical rule



Right, so far. I do understand.




I've tested it.
The number returned by will not contain an fraction-delimiter.
But the number returned by will not contain any fraction-delimiter.

This concludes, that answer D. can not be a correct answer, cause will not return 987.123456 but simply 987123456.
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This concludes, that answer D. can not be a correct answer, cause will not return 987.123456 but simply 987123456.



where you are tested it?? if you tested it under command prompt you will definately get the 987.123456 even your using
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

where you are tested it?? if you tested it under command prompt you will definately get the 987.123456 even your using
view plaincopy to clipboardprint?

1. system.out.println((Number)nf.parse(s));




I've tested both, Eclipse and console. And the ouput of this is or this

is always this: 987123456 <-- no delimiters!!!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try in this way...
public class Slice {


public static void main(String[] args) {
String s = "987.123456";
double d = 987.123456d;

Locale inLocale = new Locale("EN");
NumberFormat nf = NumberFormat.getInstance(inLocale);
nf.setMaximumFractionDigits(5);
System.out.println(nf.format(d)+ " ");
try{
System.out.println(nf.parse(s));
}
catch (Exception ex){
System.out.println("got exc");
}

}

}

I think that the way that NumberFormat prints depend on Locale language settings
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sebastian tortschanoff wrote:
is always this: 987123456 <-- no delimiters!!!


Hi sebastian,
but i am getting 987.123456 always when i run in jdeveloper as well as console.
which version of jdk your using? i am using 1.6.0
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think that the way that NumberFormat prints depend on Locale language settings



Hey Wojciech,

Excellent, this works.

It's sad, that in this case my german Locale behaves so strange. At least there should be a comma, cause this is the standard delimiter for numbers in germany.
 
Ryan Beckett
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I had originally said that you should check the locale in my previous post, but I edited it. I guess I wasn't paying attention to the output. The API says -

getInstance()
Returns the default number format for the current default locale.

-

 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Ryan, i don't wanted to ignore you. It was not so clear to me, that you've posted it.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here in Brazil with operating system in Portuguese


Remember: NumberFormat.getInstance() returns a general-purpose number format for the current default locale.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic