How can I round a calculated value server side?

How can I round a calculated value server side?

ztaylor1990ztaylor1990 Posts: 27Questions: 10Answers: 0

I am trying to round a calculated value that I am getting from a preEdit event. I need the value that is being submitted to the database to be rounded to 2 decimal places. I have tried to add the PHP round function before the setValue and it breaks the file. Do you have any suggestions for this? I don't want to use render on the client side such as .toFixed(2) because I need the value rounded in the database. Is there a way I can do this with getFormatter?

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    Answer ✓

    You say you want to submit the value to the database. So why would you use a getFormatter? Use a setFormatter please.

    Field::inst( 'yourField' )
        ->setFormatter( function ( $val, $data, $opts ) {
            return round($val, 2);
        } ),
    
  • ztaylor1990ztaylor1990 Posts: 27Questions: 10Answers: 0

    I figured it was getFormatter I just didn't know how to set it up. Thanks

This discussion has been closed.