Skip to content

Commit

Permalink
Getting numbers to print out like canonical Sass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Leung committed Feb 14, 2013
1 parent f2c959d commit 9c05850
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions node_emitters.cpp
Expand Up @@ -15,6 +15,17 @@ using std::endl;

namespace Sass {

string frac_to_string(double f, size_t p) {
stringstream ss;
ss.setf(ios::fixed, ios::floatfield);
ss.precision(p);
ss << f;
size_t offset = f < 0 ? 2 : 1;
string result = ss.str().substr(offset, p+offset);
while (result[result.size()-1] == '0') result.erase(result.size()-1, 1);
return result;
}

string Node::to_string(Type inside_of, const string space, const bool in_media_feature) const
{
if (is_null()) return "";
Expand Down Expand Up @@ -221,20 +232,33 @@ namespace Sass {

case numeric_percentage: {
stringstream ss;
ss << numeric_value();
double ipart;
double fpart = std::modf(numeric_value(), &ipart);
ss << ipart;
if (fpart != 0) ss << frac_to_string(fpart, 5);
// ss << numeric_value();
ss << '%';
return ss.str();
}

case numeric_dimension: {
stringstream ss;
ss << numeric_value() << unit().to_string();
double ipart;
double fpart = std::modf(numeric_value(), &ipart);
ss << ipart;
if (fpart != 0) ss << frac_to_string(fpart, 5);
ss << unit().to_string();
// ss << numeric_value() << unit().to_string();
return ss.str();
} break;

case number: {
stringstream ss;
ss << numeric_value();
double ipart;
double fpart = std::modf(numeric_value(), &ipart);
ss << ipart;
if (fpart != 0) ss << frac_to_string(fpart, 5);
// ss << numeric_value();
return ss.str();
} break;

Expand Down

0 comments on commit 9c05850

Please sign in to comment.