Formatting Currency with Commas


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Formatting Currency with Commas

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Location
    Hartford, CT
    Posts
    2

    Formatting Currency with Commas

    I was able to find the following script to format currency with commas but for some reason it keeps dropping the trailing zero.

    For example, if the amount is $11,750.10 it formats it as "$11,750.1".

    I've played around with this for the last day or so without any luck. I need some Java expertise as I'm a VB person.
    ===============================
    function formatCurrency(n,d) // n = number, d = delimeter
    {
    // round to 2 decimals if cents present
    n = (Math.round(n * 100) / 100).toString().split('.');
    var
    myNum = n[0].toString(),
    fmat = new Array(),
    len = myNum.length,
    i = 1, deci = (d == '.') ? ',' : '.';
    for(i; i < len + 1; i++) fmat[i] = myNum.charAt(i-1);

    fmat = fmat.reverse();
    for(i = 1; i < len; i++)
    {
    if(i % 3 == 0) {
    fmat[i] += d;
    }
    }
    var val = fmat.reverse().join('') +
    ( n[1] == null ? deci + '00' :
    (deci + n[1])
    );
    }
    return val;
    }
    =======================

    Thanks in advance!

    Kevin

  2. #2
    Join Date
    Aug 2005
    Posts
    17
    Kevin,

    Try this example. Paste it into a new java file and run it.

    Code:
    import java.text.*;
    
    class FormatExample
    {
    
    	public static void main(String[] args)
    	{
    		double myMoney = 15.2;
    		NumberFormat nf = NumberFormat.getCurrencyInstance();
    		
    		System.out.println (nf.format(myMoney));
    	
    	}	
    }
    The most important thing is to use the NumberFormat class. By calling the method getCurrencyInstance you get a formatter for currency.

    Then just call the format method of the formatter and thats it!

    For more help, www.NeedProgrammingHelp.com

Similar Threads

  1. Formatting Variables in Form
    By ortizsr in forum VB Classic
    Replies: 11
    Last Post: 06-23-2005, 08:56 AM
  2. Currency Help...decimal etc...
    By Wayne in forum .NET
    Replies: 1
    Last Post: 10-31-2002, 10:41 PM
  3. Newbie: Calculations using Currency
    By Magic in forum VB Classic
    Replies: 1
    Last Post: 03-22-2002, 08:23 PM
  4. Currency Conversion
    By Simon Jones in forum Java
    Replies: 0
    Last Post: 10-17-2001, 06:44 AM
  5. Formatting Text with XML
    By Liz in forum XML
    Replies: 4
    Last Post: 07-18-2000, 05:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center