DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  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, 07:56 AM
  2. Currency Help...decimal etc...
    By Wayne in forum .NET
    Replies: 1
    Last Post: 10-31-2002, 09:41 PM
  3. Newbie: Calculations using Currency
    By Magic in forum VB Classic
    Replies: 1
    Last Post: 03-22-2002, 07:23 PM
  4. Currency Conversion
    By Simon Jones in forum Java
    Replies: 0
    Last Post: 10-17-2001, 05:44 AM
  5. Formatting Text with XML
    By Liz in forum XML
    Replies: 4
    Last Post: 07-18-2000, 04:21 PM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links