PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 16, 2022

[FIXED] How to format print function in Java to print equal width per different number?

 September 16, 2022     formatting, java, printing     No comments   

Issue

Given these variables:

int a = 1, b = 123, c = 55, d= 1231;

Is there a way in Java to print them with a set width of 5, say. In case number is less than five digits - only print dashes.

1----,123--,55---,1231-

I am aware that these can be achieved with some loops and if statements, looking for something similar to setw() from C++


Solution

System.out.println(String.format("%-5.5s", s).replace(" ", "-"));


Answered By - mpdgr
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing