Revision: 2562
Author:
dbat...@google.com
Date: Tue Nov 19 21:35:54 2013 UTC
Log: Format small distances as fractions of mm's, not um's (Issue
1080001)
http://code.google.com/p/page-speed/source/detail?r=2562
Modified:
/lib/trunk/src/pagespeed/formatters/formatter_util.cc
/lib/trunk/src/pagespeed/formatters/formatter_util_test.cc
=======================================
--- /lib/trunk/src/pagespeed/formatters/formatter_util.cc Fri Oct 18
21:27:14 2013 UTC
+++ /lib/trunk/src/pagespeed/formatters/formatter_util.cc Tue Nov 19
21:35:54 2013 UTC
@@ -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,25 @@
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,
+ // Attempt to show a minimum of two significant figures, with a minimum
+ // result of 0.01 and truncating trailing zeroes. If the value is between
+ // 0 and 1, and the hundredth place is non-zero, then show a two digit
+ // decimal value. Otherwise, if the value is between 0 and 10 and the
tenth
+ // 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)) % 10;
+ 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) {
=======================================
--- /lib/trunk/src/pagespeed/formatters/formatter_util_test.cc Fri Oct 18
21:27:14 2013 UTC
+++ /lib/trunk/src/pagespeed/formatters/formatter_util_test.cc Tue Nov 19
21:35:54 2013 UTC
@@ -54,8 +54,11 @@
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.1mm", pagespeed::formatters::FormatDistance(100));
+ 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));