diff --git a/Classes/PHPExcel/Style/NumberFormat.php b/Classes/PHPExcel/Style/NumberFormat.php index 1b72cda92..cd91e1ea1 100644 --- a/Classes/PHPExcel/Style/NumberFormat.php +++ b/Classes/PHPExcel/Style/NumberFormat.php @@ -264,7 +264,7 @@ private static function fillBuiltInFormatCodes() // 40: "#,##0.00_);[Red](#,##0.00)" // 47: "mm:ss.0" // KOR fmt 55: "yyyy/mm/dd" - + // Built-in format codes if (is_null(self::$builtInFormats)) { self::$builtInFormats = array(); @@ -689,6 +689,8 @@ public static function toFormattedString($value = '0', $format = PHPExcel_Style_ // scale number $value = $value / $scale; + $decimalsFormat = strstr($format, '.'); + $optionalDecimalsCount = strlen($decimalsFormat) - strlen(rtrim($decimalsFormat, '#')); // Strip # $format = preg_replace('/\\#/', '0', $format); @@ -723,6 +725,11 @@ public static function toFormattedString($value = '0', $format = PHPExcel_Style_ } } } + + while ($optionalDecimalsCount > 0 && substr($value, -1) === '0') { + $value = substr($value, 0, -1); + $optionalDecimalsCount--; + } } if (preg_match('/\[\$(.*)\]/u', $format, $m)) { // Currency or Accounting diff --git a/unitTests/rawTestData/Style/NumberFormat.data b/unitTests/rawTestData/Style/NumberFormat.data index 49acaccfc..43630bf52 100644 --- a/unitTests/rawTestData/Style/NumberFormat.data +++ b/unitTests/rawTestData/Style/NumberFormat.data @@ -8,7 +8,7 @@ 0.1, "0.0", "0.1" 0.1, "0", "0" 5.5555, "0.###", "5.556" -5.5555, "0.0##", "5.556" +5.5555, "0.0##", "5.556" 5.5555, "0.00#", "5.556" 5.5555, "0.000", "5.556" 5.5555, "0.0000", "5.5555" @@ -35,3 +35,15 @@ -123456789, '0000:00:00', "-12345:67:89" 1234567.89, '0000:00.00', "12345:67.89" -1234567.89,'0000:00.00', "-12345:67.89" +12345.6789, '"#,##0.##"', '"12,345.68"' // test case from above +12345.6, '"#,##0.00"', '"12,345.60"' // 0 means: show decimal +12345.6, '"#,##0.##"', '"12,345.6"' // # means: "cut off zeros" +12345, '"#,##0.00"', '"12,345.00"' // 0 means: show decimal +12345, '"#,##0.##"', '"12,345."' // # means: "cut off zeros" (decimal separator can not be made optional by excel) +12345.6789, '"#,##0.0#"', '"12,345.68"' // first not optional, second optional +12345.6, '"#,##0.0#"', '"12,345.6"' // first not optional, second optional +12345, '"#,##0.0#"', '"12,345.0"' // first not optional, second optional +12345.6789, '"#,##0.000#"', '"12,345.6789"' +12345.6, '"#,##0.000#"', '"12,345.600"' +12345.6789, '"#,##0.0#0#"', '"12,345.6789"' // treated like .000# +12345.6, '"#,##0.0#0#"', '"12,345.600"' // treated like .000#