![]() |
Sergey Kornilov admin 12/23/2022 |
There is something here that I do not understand. What page are talking about? 'View as' settings are applied on pages like View, list, Print etc. Based on your remark "If I then save the record" it looks like you are talking about Add or Edit pages. We need you to provide the whole picture. |
S
|
Steve Seymour 12/23/2022 |
Its sounds to me that it could be the field type in the table you are using to store the value. If you want to save the exact representation across different numeric value types - perhaps you need to use a string field ? From the manual... https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html |
![]() |
john_m_craig author 12/27/2022 |
Hi, You're right, when I said I was entering values and saving I assumed you would realise I was in the Add and Edit pages. Sorry for not being more clear. So, here is the whole picture. The values appear to be showing correctly on the List page, but not when I try to Add a new record. Now, I don't know if this is a clue as to what's going on but, I've copied some of the code from this example ... https://xlinesoft.com/articles/how_to_use_calculated_fields.htm ... to calculate some of the fields on the fly as in the Add page in the Events section. I've removed the code from the Edit page Events so I can see if the problem is with my code ... var ctrlVATRate = Runner.getControl(pageid, 'Time Worked VAT Rate'); ... and if I use the '+' operator it concatenates the values to I have to multiply them by 1 to force them to be numeric. That suggests to me that the fields are in fact text strings and not numeric - until I multiply them by 1 !! So, for example, if I had values of 200 for 'Time Nett' and 40 for 'Time VAT', then 'Time Gross' should be 240 but was in fact giving me 20040. Multiplying them by 1 gives me the correcct value BUT not still not displaying 2 decimal places. Steve,thanks for your suggetion but the idea of storing numeric values in a DB as strings is a concept I can't get my head round. That said you set me to thinking I had defined my fields incorreectly and I had not. They were set to decimal(32,0) instead of decimal(32,2). I changed them but it made no difference, the values were not displaying to 2 decimal places. |
![]() |
fhumanes 12/27/2022 |
Hello, His assumption is successful. Read this article to know how to force numerical values. https://dev.to/sanchithasr/7-ways-to-convert-a-string-to-number-in-javascript-4l Regards, |
![]() |
Sergey Kornilov admin 12/27/2022 |
It is a bit difficult to understand what is the exactly issue is but I provide my input based on what I see.
For string to number conversion use Number() function. Example: For numbers rounding use Math.round(): |
![]() |
john_m_craig author 12/28/2022 |
I think I've gotten to the bottom of it and now understand the rules of the game and thanks for the responses you've contributed. First off, don't make the mistake I did and define your decimal fields as Decimal (10,0), they should be Decimal (10,2). Sorry for any confusion. I don't know how I did that !! Now, if you don't create any JavaScript Events, the View As values you set on fields to display 2 decimal places will show the 2 decimal places correctly while Adding or Editing records. BUT, if you add the Event calculations then these caculations turn the field values, while the code is running, into text and if you want them to be numeric you have to use Numeric() to convert them AND THEN you have manage the decimal display in the code as the Add or Edit page View As won't do it for you. The demo code I used to base my calculations on did not include the Number() conversion ... https://xlinesoft.com/articles/how_to_use_calculated_fields.htm ... while the example Admin provided below ... https://xlinesoft.com/phprunner/docs/how_to_calculate_values_on_the_fly.htm ... does use Number() So, Fernando, thanks for your advice which makes sense now I know the View As settings won't work if I use calculations. I've updated my code to include Number(). Thanks for your help everyone. |
![]() |
Sergey Kornilov admin 12/28/2022 |
Glad you were able to resolve it! Just to add a couple of clarifications.
|