IF statement to replace text

edited 9:32AM in FastReport VCL 5
I am trying to substitute some text. For example, if a data field returns 0.375 I want to instead show 3/8. The data field here is [WO-Part."Thickness"]. I would want to use several lines or an array something like:

(IF [WO-Part."Thickness"]==0.375, "3/8",,)
(IF [WO-Part."Thickness"]==0.250, "1/2",,)

Is this even close??

Comments

  • gpigpi
    edited 9:32AM
    [IIF(<WO-Part."Thickness">==0.375, "3/8", IIF(<WO-Part."Thickness">==0.250, "1/2",""))]
  • LurkingKiwiLurkingKiwi Wellington, New Zealand
    edited 9:32AM
    Note that absolute comparisons to floating point numbers are generally a bad idea, as a tiny difference in the calculation can result in a minute fractional difference.
    You could compare the text version of your field to "0.375", as it will have a fixed precision, or do something like ABS(<WO-Part."Thickness"> - 0.375) < 0.001
  • PolomintPolomint Australia
    edited May 2018
    I'd like to endorse Lurking K1w1's response.

    And cheekily add that 0.25 is 1/4 not 1/2 (0.50)!

    The code above only catches two cases.

    Would it not be better to create a function that presents decimal amounts as the closest vulgar fraction? There are quite a few examples of code to do this, just a google away...

    Cheers, Paul
  • edited 9:32AM
    Polomint wrote: »
    I'd like to endorse Lurking K1w1's response.

    And cheekily add that 0.25 is 1/4 not 1/2 (0.50)!

    The code above only catches two cases.

    Would it not be better to create a function that presents decimal amounts as the closest vulgar fraction? There are quite a few examples of code to do this, just a google away...

    Cheers, Paul


    OMG and I do this for a living!

    Thanks for the code! I was nowhere close. In our circumstance (still good advice for others down the road), we are very specific with our decimals, i.e. 0.375 is always written exactly that way.
  • edited 9:32AM
    I'm with LurkingKiwi on this. The problem with float does not come in the way you "write it", but in the way it is stored or manipulated which is most of the time out of the programmer's control. Comparing fixed value might be working for you at the moment, but it will without a doubt be a problem at some point. You can't rely on comparing float value without some sort of tolerance.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.