Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help formatting output  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an int population variable  that needs to be displayed as 453,000:
the comma tells me that the display is a string, i could be wrong. im using printf to format the output, but since i have an int coming in and a string(i assume) going out,
i cant find a way to cast from int to string.
Country country1 = new Country("Name",45300, 6);
   System.out.printf("Name: %s\n", country1.getName());
   System.out.printf("Population: %d\n",country1.getPopulation());

 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Go through the Formatter documentation and look at the options under %d. Beware: there is a lot to read through. There is an option which allows you to print a thousands separator, which I think is comma. You will probably want something like %,6d as your % tag. That will put commas in if you are in UK, USA, or many other English‑speaking countries. If that doesn't work, ask again and tell us which language your computer uses as its default.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the warm welcoming
Your reply led me to this https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
and i found the correct answer, thank you for steering me in the right direction.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!