• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Apply Format  RSS feed

 
ali ak
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to get Cpu speed using my GetProcessorSpeed method and It returns this output 1796 How can apply this pattern "#.##".I am tryin something like this.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should use the return value of formatter.format(...):

 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use the percent tags. There are several methods which use percent tags, but they all work the same way. See the Formatter class. One possible way to solve your problem:-
myLabel.setText(String.format("$%d.%02d", dollars, cents));
That will not work for 1796 which is a whole number, and does not appear in a format similar to 1234.56.
myLabel.setText(String.format("%0d.%03d", (int)(speed / 1000), (int)(speed % 1000 + 0.0005)));
If our speed is not a double, then the (int) casts and the + 0.0005 for rounding may be unnecessary.
 
ali ak
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you friend
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!