Java formatter and decimal format

Shivtek
Shivtek used Ask the Experts™
on
Hey Guys,

I am trying to print a variable:

            DecimalFormat a = new DecimalFormat("###.00");
            Formatter fmt = new Formatter();

System.out.printf("\tText $" + "\t\t" + a.format(totalText));

I want to put a formatter in it too so that the printed number right justifies.

Is there maybe a better way to do it.?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
a_b
Top Expert 2009

Commented:
In the formatter class the output is right justified within the given width.

Author

Commented:
how can I put a decimal format and formatter?
a_b
Top Expert 2009

Commented:
float pi = 3.1422313213;
System.out.format("%10.3f%n", pi);             //  -->  "     3.142"
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

a_b
Top Expert 2009

Commented:
I am still not clear about what output do you expect.

Here is a link on formatting - http://java.sun.com/docs/books/tutorial/java/data/numberformat.html

Author

Commented:
I want to print:

exmaple:

       Text $          4523423.00
       Link $                  656.98
Another $                  634.75

Something like this.


Top Expert 2016

Commented:
Surely you'd probably need something more like this?
curr.png

Author

Commented:
Yes would need to text right justified as well...

And how do I do that?
Top Expert 2016

Commented:
>>Yes would need to text right justified as well...

The text is *left* justified ...

Author

Commented:
How is all this done?
Top Expert 2016

Commented:
Mick BarryJava Developer
Top Expert 2010

Commented:
Doesn't look like you need a DecimalFormat at all, what a_b posted above looks list it is all you need

float pi = 3.1422313213;
System.out.format("%10.3f%n", pi);             //  -->  "     3.142"

Top Expert 2009
Commented:
Try -

public class Test
{
    public static void main(String args[])
    {
        
        float a = 4523423.00f;
        float b= 656.98f;
        float c = 634.75f;
        
        String str1  = "Text $";
        String str2 = "Link $";  
        String str3 = "Another $";
            
        System.out.format("%-20s %10.2f%n", str1,a);           
        System.out.format("%-20s %10.2f%n", str2,b);             
        System.out.format("%-20s %10.2f%n", str3,c);             
 
}}

Open in new window

Author

Commented:
What does the %n do?
a_b
Top Expert 2009

Commented:
new line

Author

Commented:
GREAT!!!  a_b, I must say your answer was THE BEST....WICKED *_*

Thanks a lot!!
Top Expert 2016

Commented:
>>I must say your answer was THE BEST

That gives the following, with the amount right-justified, but with the currency sign disassociated from the amount



Text $               4523423.00
Link $                   656.98
Another $                634.75

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial