Post

4 followers Follow
2
Avatar

How to convert Text from a text field into number type in calculation field by javascript ?

There is an app has Text fields "revenue_text"(content is "223") and "cost_text"(content is "100"),I would like to get profit in a calculation field by code below:

but ,there are error messages :
1."the result is not a valid number "
2."invalid value 123.0(number):must be string"

what's wrong with it ?

My purpose is to get string from text field ,then turn it into number in calculation field .
It seems the calculation field is text type instead of number type which is wanted by me .
How can I make it ?

The script as follows:

var a=@revenue_text ;

var b=@cost_text;

var c= parseFloat(a);
var d= parseFloat(b)
var profit=c-d;
profit;

Best

杜晓闻

Please sign in to leave a comment.

11 comments

2
Avatar

It is very important that I use the feature to achieve a little bit complex function to handle orders and invoices .I hope we can make it done .

Best

JianWen

杜晓闻 2 votes
Comment actions Permalink
0
Avatar

Once a calculation field has been created its return type is locked. In your case it looks like you have created a calculation field that returns a string and now you're trying to return a number.

Remove your current calculation field and create a new one that returns a number from the start.

/Andreas

Andreas Haugstrup Pedersen 0 votes
Comment actions Permalink
0
Avatar

Hi Andreas,

Thank you very much. It works.

Best regards

JianWen

杜晓闻 0 votes
Comment actions Permalink
0
Avatar

You are probably ending up with NaN as a value for sum at some point or for some items.

Andreas Haugstrup Pedersen 0 votes
Comment actions Permalink
0
Avatar

I'm sure "ar[i]" in the pic is a number("1","2" or "3")(not NaN), and this code works well under node.js, Can you tell me why it shows "the result is not a valid number"?

Best

JianWen

杜晓闻 0 votes
Comment actions Permalink
1
Avatar

The issue seems result from the semicolon in the end . When I deleted the semicolon ,just :"sum",instead of :"sum;",It works and shows the right sum result.

杜晓闻 1 vote
Comment actions Permalink
0
Avatar

Did you try to add a semicolon ";" or a space " "at end ? That it "Number " or "Number;",or "Number; " or "Number ;"

杜晓闻 0 votes
Comment actions Permalink
0
Avatar

杜晓闻, I've tried all four possible combinations from blank space and semicolon. Always the same result.

Fritz Feger 0 votes
Comment actions Permalink
0
Avatar

I checked my notes :It was number field and all right when it's "profit;"in the last line ,but later (maybe a few days later ),it showed "the result is not a valid number", then I tried a lot ,but it only worked as "profit" (deleted the semicolon ).
good luck

杜晓闻 0 votes
Comment actions Permalink