• Resolved jimdandy2

    (@jimdandy2)


    I’d like to know if there’s a way through the Custom CSS or Custom Command to have negative numbers displayed with parentheses vs having a hyphen in front.

    (12345.00) vs -12345.00

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    I see what you mean, but from everything I know, the CSS language does not have features for parsing and transforming data/content like this, sorry. ??

    Regards,
    Tobias

    Thread Starter jimdandy2

    (@jimdandy2)

    Many thanks…I am able to do it via the CSV file during import. I was hoping to be able to do it with files from sources where I don’t control the formatting.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    you could actually use a rather simple way in PHP here, using a filter hook function. For example, add this to the end of your theme’s “functions.php” file to automatically convert all negative numbers:

    add_filter( 'tablepress_cell_content', 'jd_tablepress_negative_with_parentheses', 10, 4 );
    function jd_tablepress_negative_with_parentheses( $cell_content, $table_id, $row, $col ) {
      if ( ! in_array( $table_id, array( '123', '456' ), true ) ) {
        return $cell_content;  
      }
      
      if ( is_numeric( $cell_content) && (double) $cell_content < 0 ) {
        $cell_content = '(' . ( -$cell_content ) . ')';
      }
    
      return $cell_content;
    }

    The array list with the entries '123', '456' resembles the table IDs for which this conversion should be performed.

    Regards,
    Tobias

    Thread Starter jimdandy2

    (@jimdandy2)

    Got it, thanks…I like this approach.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! ?? Good to hear that this helped!

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display of negative numbers in parentheses’ is closed to new replies.