The number format should include a space separator for thousands 1 000.00 10 000.00 100 000.00

suuriyas
Tera Contributor

HI Community,

 

I have requirement in catalog form we have fields currency, amount and amount in eur. based on the currency and amount  selected data will convert and populate in amount in eur variable. amount in eur value is showing as 648899.73 it should be 648 899.73 

i need the number format should include a space separator for thousands, in addition to the dot for decimals - for example: 1 000.00 10 000.00 100 000.00 1 000 000.00

suuriyas_0-1743072480375.png

onchange client script:

variable name : amount

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading ) {
      return;
   }

    g_form.clearValue('amount_in_eur');
    if( newValue == '')
    {
        return;
    }
   
   var nbrwn = g_form.getIntValue('amount');

    var amt = g_form.getValue("amount");
    var curr = g_form.getValue('currency');

    if ((isNaN(newValue) == true) || (nbrwn < 0)) {
        alert("This field should contain only numeric values! (No spaces or letters)");
        g_form.setValue('amount', '');
        g_form.setValue('amount_in_eur', '');

    } else {
        var ajax = new GlideAjax('Currency_Conversion_Utils');
        ajax.addParam('sysparm_name', 'conversionforNewPaymentForms');
        ajax.addParam('sysparm_currency', curr);
        ajax.addParam('sysparm_amount', amt);
        ajax.getXML(doSomething);
    }

    function doSomething(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
            g_form.setValue('amount_in_eur', answer);
        }
       
   
}

script include:

var Currency_Conversion_Utils = Class.create();
Currency_Conversion_Utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    conversionforNewPaymentForms: function() {

        var u_curr = this.getParameter('sysparm_currency');
        var u_amt = this.getParameter('sysparm_amount');

        var fxcurrency = new GlideRecord('fx_currency');
        fxcurrency.addQuery('sys_id', u_curr);
        fxcurrency.query();
        if (fxcurrency.next()) {

            var fxrate = new GlideRecord('fx_rate');
            fxrate.addQuery('currency', fxcurrency.getUniqueValue());
            fxrate.orderBy('currency');
            fxrate.addEncodedQuery("currency.active=true^sys_created_onONYesterday@javascript&colon;gs.daysAgoStart(1)@javascript&colon;gs.daysAgoEnd(1)");
            fxrate.query();

            if (fxrate.next()) {
                var rate = fxrate.rate;
                var amtineuro = u_amt / rate;
                var roundValue = parseFloat(amtineuro).toFixed(2);
                return roundValue.toString();
            } else {
                return "Exchange not found";
            }

        } else {
            return "Exchange not found";
        }

    }
});
 
How can we achieve this?
 
Thanks in Advance
1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @suuriyas 
Inyou script include before returning the value just use the below regex pattern.

var Currency_Conversion_Utils = Class.create();
Currency_Conversion_Utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    conversionforNewPaymentForms: function() {

        var u_curr = this.getParameter('sysparm_currency');
        var u_amt = this.getParameter('sysparm_amount');

        var fxcurrency = new GlideRecord('fx_currency');
        fxcurrency.addQuery('sys_id', u_curr);
        fxcurrency.query();
        if (fxcurrency.next()) {

            var fxrate = new GlideRecord('fx_rate');
            fxrate.addQuery('currency', fxcurrency.getUniqueValue());
            fxrate.orderBy('currency');
            fxrate.addEncodedQuery("currency.active=true^sys_created_onONYesterday@javascript&colon;gs.daysAgoStart(1)@javascript&colon;gs.daysAgoEnd(1)");
            fxrate.query();

            if (fxrate.next()) {
                var rate = fxrate.rate;
                var amtineuro = u_amt / rate;
                var roundValue = parseFloat(amtineuro).toFixed(2);
// ---------------START----------------------------------
				var str = roundValue.toString();
                return str.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
//-----------------END-----------------------------------
            } else {
                return "Exchange not found";
            }

        } else {
            return "Exchange not found";
        }

    }
});

 

Hope this helps.

Regards,

Siva

View solution in original post

4 REPLIES 4

Medi C
Giga Sage

Hello @suuriyas,

 

Please ensure you are using String variable for the "amount in eur" and adjust your client script by adding a new function to format your number and use it when setting the value as follow: 

 function doSomething(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
            g_form.setValue('amount_in_eur', formatNumber(answer)); //Setting the formatted number
 }
function formatNumber(num) {
    if (typeof num !== "number") {
        return "Invalid input";
    }
    
    var parts = num.toString().split(".");
    var integerPart = parts[0];
    var decimalPart = parts.length > 1 ? "." + parts[1] : "";

    var formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
    
    return formattedInteger + decimalPart;
}

 

I have tested the formatNumber format and it is working as expected:

gs.log(formatNumber(648899.73)); // Output: "648 899.73"
gs.log(formatNumber(123456789)); // Output: "123 456 789"

 

What I did not get is "1 000.00 10 000.00 100 000.00 1 000 000.00". Could you explain what are the requirements in here?

 

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

suuriyas
Tera Contributor

HI @Medi C ,

 

I tried the script but it is showing as invalid

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading ) {
      return;
   }

    g_form.clearValue('amount_in_eur');
    if( newValue == '')
    {
        return;
    }
   
   var nbrwn = g_form.getIntValue('amount');

    var amt = g_form.getValue("amount");
    var curr = g_form.getValue('currency');

    if ((isNaN(newValue) == true) || (nbrwn < 0)) {
        alert("This field should contain only numeric values! (No spaces or letters)");
        g_form.setValue('amount', '');
        g_form.setValue('amount_in_eur', '');

    } else {
        var ajax = new GlideAjax('Currency_Conversion_Utils');
        ajax.addParam('sysparm_name', 'conversionforNewPaymentForms');
        ajax.addParam('sysparm_currency', curr);
        ajax.addParam('sysparm_amount', amt);
        ajax.getXML(doSomething);
    }

    function doSomething(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
            g_form.setValue('amount_in_eur', formatNumber(answer)); //Setting the formatted number
 }
function formatNumber(num) {
    if (typeof num !== "number") {
        return "Invalid input";
    }
   
    var parts = num.toString().split(".");
    var integerPart = parts[0];
    var decimalPart = parts.length > 1 ? "." + parts[1] : "";

    var formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
   
    return formattedInteger + decimalPart;
}
       
   
}
suuriyas_0-1743074429023.png

 

Amount in eur variable is single line text only, these are just an examples 1 000.00 10 000.00 100 000.00       1 000 000.00 for example if the value is thousand then it should be like 1 000.00 and not like 1000.00 similarly it should be like 10 000.00 and not like 10000.00

 

Shivalika
Mega Sage

Hello @suuriyas 

 

You don't need all this scripting. 

 

Convert that field to string. And under the Type Specification you would be having Validation regex. 

 

Create a new Validation regex with below 👇 patter - 

(?<!\d)(\d{1,3})(?=(\d{3})+(?:\.\d{2})?\b)

 

Please try and let me know if modifications needed I will make it. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

J Siva
Tera Sage

Hi @suuriyas 
Inyou script include before returning the value just use the below regex pattern.

var Currency_Conversion_Utils = Class.create();
Currency_Conversion_Utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    conversionforNewPaymentForms: function() {

        var u_curr = this.getParameter('sysparm_currency');
        var u_amt = this.getParameter('sysparm_amount');

        var fxcurrency = new GlideRecord('fx_currency');
        fxcurrency.addQuery('sys_id', u_curr);
        fxcurrency.query();
        if (fxcurrency.next()) {

            var fxrate = new GlideRecord('fx_rate');
            fxrate.addQuery('currency', fxcurrency.getUniqueValue());
            fxrate.orderBy('currency');
            fxrate.addEncodedQuery("currency.active=true^sys_created_onONYesterday@javascript&colon;gs.daysAgoStart(1)@javascript&colon;gs.daysAgoEnd(1)");
            fxrate.query();

            if (fxrate.next()) {
                var rate = fxrate.rate;
                var amtineuro = u_amt / rate;
                var roundValue = parseFloat(amtineuro).toFixed(2);
// ---------------START----------------------------------
				var str = roundValue.toString();
                return str.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
//-----------------END-----------------------------------
            } else {
                return "Exchange not found";
            }

        } else {
            return "Exchange not found";
        }

    }
});

 

Hope this helps.

Regards,

Siva