LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2009, 04:41 AM   #1
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Format a number in Java to be three characters wide


I am using Java (for my sins) and want to display a number to always use three characters, but no joy.

Some snippets of what I have tried...
Code:
String msg = MessageFormat.format("{0,number,##0}%", (double) _size
            * TO_PERCENTAGE / containerSize);
Code:
NumberFormat percent = NumberFormat.getPercentInstance();
String msg = percent.format((double)_size * TO_PERCENTAGE / containerSize);
Code:
DecimalFormat decimal = new DecimalFormat("##0");
String msg = decimal.format(_size * TO_PERCENTAGE / containerSize)
            + "%";
Each work the same, which is print the number but left aligned without the spaces that I yearn for.

Is there any way to entice this behaviour out of the Format family of classes or do I have to do the padding myself?
 
Old 06-29-2009, 08:48 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Java, actually, has 'printf' - isn't it sufficient ?
 
Old 06-29-2009, 10:55 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Original Poster
Rep: Reputation: 148Reputation: 148
Thanks for the hint printf() doesn't really help because I want the result in a String, but with a bit more searching I found that the following works:

Code:
String msg = String.format("%3d", _size * TO_PERCENTAGE / containerSize) + "%";

Last edited by graemef; 06-29-2009 at 10:57 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed: replace same number of characters between tags unihiekka Linux - Newbie 6 12-30-2008 03:51 AM
Counting number of characters in a file (but not as simple as wc) bioinformatics_guy Linux - Newbie 14 09-18-2008 08:15 PM
grepping 'how many words have x number of characters' dtat651 Linux - Newbie 17 07-27-2008 11:24 PM
Configuring 22" wide format LCD OldAl Linux - Hardware 26 03-06-2008 10:37 PM
LXer: Programming with wide characters LXer Syndicated Linux News 0 02-11-2006 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration