Field Number format

deant

Active Member
In invoice form I have set up filed formatting: Format = decimal, NumberFormat to Yes, dot(.) as Thousand separator and comma (,) ad Decimal separator. I do all calculations in JS. Everything works fine but after saving the form all the values change in both the table and the form.
Here is sxample:
Price : 12,00 transforms to 1.200,00 ... in db 1200.00 (db format: decimal(8,2)

I get value from element:
var elem = parseFloat(form.formElements.get('invoice___pricej').getValue());
I update element:
form.formElements.get('invoice___total').update(total.toFixed(2));

How to set up elemnnts to show EU number format correctly an form and on pdf?
 
Last edited:
In the DB it's always a dot (and not a comma) as decimal point, no matter what you do client-side with JS, so you must keep in mind that a value of "12,00" will - naturally - be stored as "1200.00" or "1200" in the database (depending on settings).
 
Yes but...if user type price like 12,00 in db is 12.00 when reopen form is 12,00 which is OK.

The value changes in the case when I programmatically (JS code/ajax result) put the value in the field or enter the calculation in the field. Maybe is problem in JS number format .toFixed()?
 
In past week I was tested formating numbers with all possible different settings for field like decimal/text, number format, mask etc. I try several 3rd party js librarys also but I was still not satisfied. Whether it was too complicated or required too much custom coding or the format conversion was not right...

The simplest solution worked best. Here it is for eurpean number format e.g.1.234,56

Filed setings: Format = text; Number format = NO ...in db table I store string(varchar) not a decimal number. Some programes says: If you dealing with numbers then store numbers in db...other says it does not matter for smaller systems where accuracy is not required...mean more then 3 or 4 decimal and converting not slowing down system.

With Jqurey prevent user to input dot as thousand or decimal separator:
JavaScript:
Fabrik.addEvent('fabrik.form.elements.added', function(elements) {
        jQuery('.input-small').keyup(function() {
            this.value = this.value.replace(/[^0-9\,]/g, '');
        });
    });

Then I use two functions for conversation.
First one is to convert value from field and transform to float so I can do all kind of calculations in js file
JavaScript:
function price_to_number(v) {
    if (!v) { return 0; }
    v = v.split('.').join('');
    v = v.split(',').join('.');
    return Number(v.replace(/[^0-9.]/g, ""));
}
Second one is convert number back to price and put it in a filed and stor in db
JavaScript:
function number_to_price(v) {
    if (v == 0) { return '0,00'; }
    v = parseFloat(v);
    v = v.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    v = v.split('.').join('*').split(',').join('.').split('*').join(',');
    return v;
}

How to use it:

JavaScript:
var price = form.formElements.get(price).getValue();
var price_number = price_to_number(price);

var result = (a * (b / 100) + 1,25);
update: document.id('table_element').value = number_to_price(result);
My approach is maybe wrong so your opinions are welcome.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top