简体   繁体   English

从字符串中删除尾随零

[英]Remove trailing zeros from a string

I would like to remove trailing zero's according to the following rules: 我想根据以下规则删除尾随零:

  • The input number will always have 4 decimal places 输入数字将始终具有4个小数位
  • The output number should always have between 2 and 4 decimals 输出数字应始终在2到4位小数之间
  • If the last two digits after the decimal is a zero, then remove both of them 如果小数点后的最后两位数为零,则删除它们
  • If the last digit after the decimal is a zero, then remove it 如果小数点后的最后一位数为零,则将其删除

Examples: 例子:

  • 1.0000 -> 1.00 1.0000 - > 1.00
  • 1.4000 -> 1.40 1.4000 - > 1.40
  • 1.4100 -> 1.41 1.4100 - > 1.41
  • 1.4130 -> 1.413 1.4130 - > 1.413
  • 1.4136 -> 1.4136 1.4136 - > 1.4136

I have considered the following: 我考虑过以下几点:

if (substr($number, -2) == '00') return substr($number, 0, 4);
if (substr($number, -1) == '0') return substr($number, 0, 5);
return $number;

Are there any better ways of doing this? 有没有更好的方法来做到这一点?

I think this should work: 我认为这应该有效:

return preg_replace('/0{1,2}$/', '', $number);

$strings = array ('1.4000', '1.4100', '1.4130', '1.4136', '1.4001', '1.0041');
foreach ($strings as $number) {
  echo "$number -> " . preg_replace('/0{0,2}$/', '', $number) . "\n";
}

Produces: 生产:

1.4000 -> 1.40
1.4100 -> 1.41
1.4130 -> 1.413
1.4136 -> 1.4136
1.4001 -> 1.4001
1.0041 -> 1.0041

Here's a variation on Barmar's solution that will work even if the input strings don't always have exactly 4 decimal places. 这是Barmar解决方案的变体,即使输入字符串并不总是精确到4位小数,它也能正常工作。 It won't add missing zeroes, but it won't strip any more than it should either. 它不会添加缺失的零,但它不会剥离任何超过它应该。

return preg_replace('/(\.[0-9]{2,}?)0*$/', '$1', $number);

As an example: 举个例子:

$strings = array(
    '1.4000',
    '1.4100',
    '1.4130',
    '1.4136',
    '1.0',
    '1.00',
    '1.000',
    '10000',
);
foreach ($strings as $number) {
    $fixed = preg_replace('/(\.[0-9]{2,}?)0*$/', '$1', $number);
    echo "{$number} -> {$fixed}\n";
}

Outputs: 输出:

1.4000 -> 1.40
1.4100 -> 1.41
1.4130 -> 1.413
1.4136 -> 1.4136
1.0 -> 1.0
1.00 -> 1.00
1.000 -> 1.00
10000 -> 10000

And now for something completely different... 而现在,完全不同的东西......

<?php
$crlf = "\n";
$input = array('1.4000', '1.4300', '1.2340', '1.4012', '1.0034', '1.2', '1.23456');
foreach ($input as $in) {
  for($n = 4; $n>2; $n--) {
    $f1 = (float)round($in, $n);
    $f2 = (float)round($in, $n-1);
    if ($f1 !== $f2) break;
  }
  echo "for in = ".$in." out is ".number_format($in, $n) . $crlf;
}
?>

Output: 输出:

for in = 1.4000 out is 1.40
for in = 1.4300 out is 1.43
for in = 1.2340 out is 1.234
for in = 1.4012 out is 1.4012
for in = 1.0034 out is 1.0034
for in = 1.2 out is 1.20
for in = 1.23456 out is 1.2346

Note that this will behave as expected for properly formatted numbers (with exactly four digits after the decimal), but it will still behave when the numbers were not correctly formatted. 请注意,对于格式正确的数字(小数点后正好有四位数),这将按预期运行,但在数字格式不正确时仍然会出现这种情况。 Not quite what was asked for - but it's often better to have a robust solution than a minimalist one... 不是所要求的 - 但是拥有一个强大的解决方案往往比一个极简主义的解决方案更好......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM