note 53651 deleted from function.number-format by cmb

From: Date: Sun, 08 Sep 2019 10:20:51 +0000
Subject: note 53651 deleted from function.number-format by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212131@lists.php.net to get a copy of this message
Note Submitter: venimus777 at yahoo dot com ---- You could use the following regular expression to divide a number into parts: $1-number without fractal part $2-fractal part $3-first 2 digits of the fractal part $4-rest of the fractal part the regex removes any leading and trailing symbols and leading zeros. It doesnt validate the number, so 12 41 is considered to be correct input! english notation: /^.*?[0]*([\d\s]+)(([\.][\d]{0,2})([\d]*))?.*?$/ french notation: /^.*?[0]*([\d\s]+)(([\,][\d]{0,2})([\d]*))?.*?$/ <?php // truncate the fractal part up to 2 digits of an "english number": $number = '01,234.50789'; $trunc = preg_replace( '/^.*?[0]*([\d\,]+)(([\.][\d]{0,2})([\d]*))?.*?$/', '$1$3', $number ); echo $trunc; ?> Outputs: 1,234.50 $number='e00012 41.100001e-4fasfd'; would output: 12 41.10

« previous php.notes (#212131) next »