Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5977325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:21:16+00:00 2026-05-22T21:21:16+00:00

I have a number which I needed to format as currency, to do this

  • 0

I have a number which I needed to format as currency, to do this im having to turn my number into a string and run a function, This is working but its showing to X decimal places, Is it possible to use ‘toFixed’ on a string at all? I’ve tried with no luck and im unsure how to turn the string back into a number, I’ve used parseInt only it stops at the first character as it doesnt read past my delimeter…

var amount = String(EstimatedTotal);
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
    var nn = n.substr(n.length-3);
    a.unshift(nn);
    n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;

at the moment the amount variable showing as 1,234,567.890123

Thanks for the help all ,

Managed to get it working with this

amount = String(EstimatedTotal) 

 amount += '';
x = amount.split('.');
x1 = x[0];
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
num=x1 ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:21:17+00:00Added an answer on May 22, 2026 at 9:21 pm

    Not sure what you mean by currency-

    this adds commas for thousands separators and forces two decimal places

    input can be a string of digits with or without commas, minus sign and decimal, or a number

    function addCommas2decimals(n){
        n= Number(String(n).replace(/[^-+.\d]+/g, ''));
        if(isNaN(n)){
            throw new Error('Input must be a number');
        }
        n= n.toFixed(2);
        var rx=  /(\d+)(\d{3})/;
        return n.replace(/^\d+/, function(w){
            while(rx.test(w)){
                w= w.replace(rx, '$1,$2');
            }
            return w;
        });
    
    }
    var s= '1234567.890123'
    addCommas2decimals(s)
    
    /*  returned value: (String)
    1,234,567.89
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of VC 6.0 projects (dsps) which build into dlls which
I have a number of strings which contain words which are bunched together and
I have a number of links which I would like to get some information
For my webpage, I have a number of lists which I'm generating dynamically from
I have a lengthy number-crunching process which takes advantage of quite abit of OpenGL
I have a number of elements in my XAML which define Foreground=#555 to be
I have a number of CSS attributes and Classes which get applied based on
I have a number of enums in my application which are used as property
I have developed a number of classes which manipulate files in Java. I am
I have a large number of objects which are identified by names (strings). So,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.