@@ -519,82 +519,41 @@ namespace Sass {
519
519
void Inspect::operator ()(Number_Ptr n)
520
520
{
521
521
522
- std::string res;
523
-
524
522
// reduce units
525
523
n->reduce ();
526
524
527
- // check if the fractional part of the value equals to zero
528
- // neat trick from http://stackoverflow.com/a/1521682/1550314
529
- // double int_part; bool is_int = modf(value, &int_part) == 0.0;
530
-
531
- // this all cannot be done with one run only, since fixed
532
- // output differs from normal output and regular output
533
- // can contain scientific notation which we do not want!
534
-
535
- // first sample
536
525
std::stringstream ss;
537
- ss.precision (12 );
538
- ss << n->value ();
539
-
540
- // check if we got scientific notation in result
541
- if (ss.str ().find_first_of (" e" ) != std::string::npos) {
542
- ss.clear (); ss.str (std::string ());
543
- ss.precision (std::max (12 , opt.precision ));
544
- ss << std::fixed << n->value ();
545
- }
546
-
547
- std::string tmp = ss.str ();
548
- size_t pos_point = tmp.find_first_of (" .," );
549
- size_t pos_fract = tmp.find_last_not_of (" 0" );
550
- bool is_int = pos_point == pos_fract ||
551
- pos_point == std::string::npos;
526
+ ss.precision (opt.precision );
527
+ ss << std::fixed << n->value ();
552
528
553
- // reset stream for another run
554
- ss. clear (); ss. str ( std::string () );
529
+ std::string res = ss. str ();
530
+ int s = res. length ( );
555
531
556
- // take a shortcut for integers
557
- if (is_int )
532
+ // delete trailing zeros
533
+ for (s = s - 1 ; s > 0 ; --s )
558
534
{
559
- ss.precision (0 );
560
- ss << std::fixed << n->value ();
561
- res = std::string (ss.str ());
562
- }
563
- // process floats
564
- else
565
- {
566
- // do we have have too much precision?
567
- if (pos_fract < opt.precision + pos_point)
568
- { ss.precision ((int )(pos_fract - pos_point)); }
569
- else { ss.precision (opt.precision ); }
570
- // round value again
571
- ss << std::fixed << n->value ();
572
- res = std::string (ss.str ());
573
- // maybe we truncated up to decimal point
574
- size_t pos = res.find_last_not_of (" 0" );
575
- // handle case where we have a "0"
576
- if (pos == std::string::npos) {
577
- res = " 0.0" ;
578
- } else {
579
- bool at_dec_point = res[pos] == ' .' ||
580
- res[pos] == ' ,' ;
581
- // don't leave a blank point
582
- if (at_dec_point) ++ pos;
583
- res.resize (pos + 1 );
584
- }
535
+ if (res[s] == ' 0' ) {
536
+ res.erase (s, 1 );
537
+ }
538
+ else break ;
585
539
}
586
540
541
+ // delete trailing decimal separator
542
+ if (res[s] == ' .' ) res.erase (s, 1 );
543
+
587
544
// some final cosmetics
588
545
if (res == " 0.0" ) res = " 0" ;
589
546
else if (res == " " ) res = " 0" ;
590
547
else if (res == " -0" ) res = " 0" ;
591
548
else if (res == " -0.0" ) res = " 0" ;
592
549
else if (opt.output_style == COMPRESSED)
593
550
{
594
- // check if handling negative nr
595
- size_t off = res[0 ] == ' -' ? 1 : 0 ;
596
- // remove leading zero from floating point in compressed mode
597
- if (n->zero () && res[off] == ' 0' && res[off+1 ] == ' .' ) res.erase (off, 1 );
551
+ if (n->zero ()) {
552
+ // check if handling negative nr
553
+ size_t off = res[0 ] == ' -' ? 1 : 0 ;
554
+ // remove leading zero from floating point in compressed mode
555
+ if (res[off] == ' 0' && res[off+1 ] == ' .' ) res.erase (off, 1 );
556
+ }
598
557
}
599
558
600
559
// add unit now
0 commit comments