How to handle fractions on the forms and convert them to decimals?

103 views
Skip to first unread message

Aditya Duggal

unread,
Apr 20, 2013, 1:37:54 AM4/20/13
to erpnext-dev...@googlegroups.com
Hi,

I have been trying to figure out a way to capture data which is in fraction but honestly cannot really figure out which field to use and how to keep fractional and decimal data in the database.

Now my problem is that I am trying to capture some dimensions and as all of you might be aware that dimensions can be in various units but in my industry the dimensions are either in mm or inch.

Now till now we are capturing all the data in mm and not in inch we generally convert the inch data manually but this creates a lot of trouble. Now entering mm data is simple which happens to be a float but when it comes to Inch data capturing we  are talking about fractions as well. Is it possible to capture the data in a form in Inch aka fractions and then save both the data in the table.

For example: we need to enter 1/2" then there are 2 ways of doing it:
  • Easier way: Make a form with float input field and ask the user to convert the fraction into decimal and then enter the data.
    • Benefit of this method is only one which is less coding for the developer.
  • Tougher way: Make a form with FRACTION input field and ask the user to enter the fraction only, now there would be another field in decimal which would be hidden from the user and the code would be run to convert the fraction to the decimal and both the values would be saved.

Now my question is that how could be handle such a situation in erpnext.

Anand Doshi

unread,
Apr 20, 2013, 2:04:52 AM4/20/13
to erpnext-dev...@googlegroups.com
Hi Aditya,

Create a custom field of type Data. Say the fieldname is field_fraction.
Assuming that the decimal fieldname is field_decimal

Now, write a function like:
cur_frm.cscript.field_fraction = function(doc) {
// suppose value of field_fraction = 4/5
// then value of field_decimal will be set as 0.8
cur_frm.set_value(field_decimal, eval(doc.field_fraction));
}

Thanks,
Anand.

--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To post to this group, send email to erpnext-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/erpnext-developer-forum/-/aberBNen3DQJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aditya Duggal

unread,
Apr 20, 2013, 6:09:53 AM4/20/13
to erpnext-dev...@googlegroups.com
Hi Anand,

Thanks for the quick response, I have tried the below code replacing the requisite quotes but there seems to be nothing happening, do I need to call this function from another function or it should work without being called. The part of code I am using is below:


cur_frm.cscript.height_dia_inch = function(doc) {
// suppose value of field_fraction = 4/5
// then value of field_decimal will be set as 0.8
cur_frm.set_value(doc.height_dia, eval(doc.height_dia_inch));
}

On Saturday, April 20, 2013 11:34:52 AM UTC+5:30, Anand Doshi wrote:
Hi Aditya,

Create a custom field of type Data. Say the fieldname is field_fraction.
Assuming that the decimal fieldname is field_decimal

Now, write a function like:
cur_frm.cscript.field_fraction = function(doc) {
// suppose value of field_fraction = 4/5
// then value of field_decimal will be set as 0.8
cur_frm.set_value(field_decimal, eval(doc.field_fraction));
}

Thanks,
Anand.
On 20-Apr-2013, at 11:07 AM, Aditya Duggal <aditya...@gmail.com> wrote:

Hi,

I have been trying to figure out a way to capture data which is in fraction but honestly cannot really figure out which field to use and how to keep fractional and decimal data in the database.

Now my problem is that I am trying to capture some dimensions and as all of you might be aware that dimensions can be in various units but in my industry the dimensions are either in mm or inch.

Now till now we are capturing all the data in mm and not in inch we generally convert the inch data manually but this creates a lot of trouble. Now entering mm data is simple which happens to be a float but when it comes to Inch data capturing we  are talking about fractions as well. Is it possible to capture the data in a form in Inch aka fractions and then save both the data in the table.

For example: we need to enter 1/2" then there are 2 ways of doing it:
  • Easier way: Make a form with float input field and ask the user to convert the fraction into decimal and then enter the data.
    • Benefit of this method is only one which is less coding for the developer.
  • Tougher way: Make a form with FRACTION input field and ask the user to enter the fraction only, now there would be another field in decimal which would be hidden from the user and the code would be run to convert the fraction to the decimal and both the values would be saved.

Now my question is that how could be handle such a situation in erpnext.


--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Anand Doshi

unread,
Apr 20, 2013, 6:19:09 AM4/20/13
to erpnext-dev...@googlegroups.com
Make sure that height_dia_inch is of type Data and height_dia is if type float. 

When you enter value in height_dia_inch as a fraction. The code will set its decimal value in height_dia. 

I have tried this code. Maybe you didn't clear cache. 

Sent from my phone
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.

To post to this group, send email to erpnext-dev...@googlegroups.com.

Anand Doshi

unread,
Apr 20, 2013, 6:20:25 AM4/20/13
to erpnext-dev...@googlegroups.com
By any chance, are these fields located in a table? The code I have given works for main form and doesn't work in table. It would be different for table. 

Sent from my phone

On 20-Apr-2013, at 3:39 PM, Aditya Duggal <aditya...@gmail.com> wrote:

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.

To post to this group, send email to erpnext-dev...@googlegroups.com.

Nabin Hait

unread,
Apr 20, 2013, 6:28:44 AM4/20/13
to erpnext-dev...@googlegroups.com
Hi,

The set_value part should be like this:

cur_frm.set_value("height_dia", eval(doc.height_dia_inch));


Regards,
Nabin Hait

Aditya Duggal

unread,
Apr 20, 2013, 6:47:06 AM4/20/13
to erpnext-dev...@googlegroups.com
You are correct Nabin, I was missing the quotes in my code and also referring it with doc, thanks alot for the same. But this has given rise to another question that is I am unable to use the following code since math is not defined.

cur_frm.set_value("height_dia", math.round(eval(doc.height_dia_inch)*25400)/1000);

@Anand: yes thes fields are in the doctype....I did not knew that we could add fields in the form which are not part of the doctype (table)

To view this discussion on the web visit https://groups.google.com/d/msg/erpnext-developer-forum/-/uAMUcW26bqkJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.
To post to this group, send email to erpnext-dev...@googlegroups.com.

Nabin Hait

unread,
Apr 20, 2013, 6:50:08 AM4/20/13
to erpnext-dev...@googlegroups.com
It should be Math.round(), case sensitive.

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.

To post to this group, send email to erpnext-dev...@googlegroups.com.

Anand Doshi

unread,
Apr 20, 2013, 6:52:41 AM4/20/13
to erpnext-dev...@googlegroups.com, erpnext-dev...@googlegroups.com
It should be Math with M capital. 

Math.round will round the number to nearest integer. It won't round the number to nearest decimal.

If you want to restrict output to 2 decimals, use flt(val, 2)

Sent from my phone
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.

To post to this group, send email to erpnext-dev...@googlegroups.com.

Aditya Duggal

unread,
Apr 20, 2013, 6:58:03 AM4/20/13
to erpnext-dev...@googlegroups.com
OMG I would really have to get used to the case sensitivity of coding. Thanks a lot for pointing out this silly mistake.

Aditya Duggal

unread,
Apr 20, 2013, 6:59:40 AM4/20/13
to erpnext-dev...@googlegroups.com
Thanks Anand

For introducing me to flt but I have already done the calculation in the round to get me 3 decimal places. Once again thank you all for the help.
Reply all
Reply to author
Forward
0 new messages