Reviewers: mdsteele,
Please review this at
http://page-speed-codereview.appspot.com/1080001/
Affected files:
M pagespeed/formatters/formatter_util.cc
M pagespeed/formatters/formatter_util_test.cc
Index: pagespeed/formatters/formatter_util_test.cc
===================================================================
--- pagespeed/formatters/formatter_util_test.cc (revision 2561)
+++ pagespeed/formatters/formatter_util_test.cc (working copy)
@@ -54,8 +54,10 @@
TEST(FormatterUtilTest, Distance) {
EXPECT_EQ("0mm", pagespeed::formatters::FormatDistance(-10));
EXPECT_EQ("0mm", pagespeed::formatters::FormatDistance(0));
- EXPECT_EQ("123um", pagespeed::formatters::FormatDistance(123));
- EXPECT_EQ("999um", pagespeed::formatters::FormatDistance(999));
+ EXPECT_EQ("0.01mm", pagespeed::formatters::FormatDistance(1));
+ EXPECT_EQ("0.12mm", pagespeed::formatters::FormatDistance(123));
+ EXPECT_EQ("0.13mm", pagespeed::formatters::FormatDistance(126));
+ EXPECT_EQ("1mm", pagespeed::formatters::FormatDistance(999));
EXPECT_EQ("1mm", pagespeed::formatters::FormatDistance(1000));
EXPECT_EQ("4.6mm", pagespeed::formatters::FormatDistance(4567));
EXPECT_EQ("3.9mm", pagespeed::formatters::FormatDistance(3949));
Index: pagespeed/formatters/formatter_util.cc
===================================================================
--- pagespeed/formatters/formatter_util.cc (revision 2561)
+++ pagespeed/formatters/formatter_util.cc (working copy)
@@ -44,7 +44,6 @@
};
const UnitDescriptor kDistances[] = {
- { 1000, "um" },
{ 1000, "mm" },
{ 1000, "m" },
{ -1, "km" },
@@ -119,7 +118,9 @@
if (micrometers <= 0) {
return "0mm";
}
- double distance = micrometers;
+
+ // The smallest unit used for output is a millimeter, not a micrometer.
+ double distance = micrometers / 1000.0;
const char* display_name = "";
for (size_t i = 0; i < kDistancesSize && distance > 0; ++i) {
const UnitDescriptor *desc = kDistances + i;
@@ -130,9 +131,23 @@
distance /= desc->quantity;
}
- // If the value is between 0 and 10, and the first decimal place is
- // non-zero, then show a single digit decimal value. Otherwise,
- // round to the nearest whole number.
+ // If the value is less than 0, and the second decimal place is non-zero,
+ // then show a two digit decimal value. Otherwise, if the value is
between
+ // 0 and 10 and the first decimal place is non-zero, then show a single
digit
+ // decimal value. Otherwise, round to the nearest whole number.
+ if (distance < 1) {
+ if (distance <= 0.01) {
+ // Make sure that the formatted version of any non-zero distance is
+ // non-zero by rounding up very small numbers.
+ return StringPrintf("0.01%s", display_name);
+ }
+ const int hundredths = static_cast<int>(round(distance * 100)) % 100;
+ if (hundredths > 0) {
+ // Round to nearest hundredth.
+ double rounded_distance = round(distance * 100) / 100;
+ return StringPrintf("%.2f%s", rounded_distance, display_name);
+ }
+ }
if (distance < 10) {
const int tenths = static_cast<int>(round(distance * 10)) % 10;
if (tenths > 0) {