简体   繁体   中英

How to use this js svg parser to change the path's decimal places in an svg file or element to n decimals?

There is this svg parser that does few svg optimization including changing an svg path's decimal places. if you use snap svg and make a path element :

var paper = Snap("100%","100%").attr({"viewbox":"0,0,500,600"});
var pth = paper.path("M89.86111111111111,140.00000000000003C89.30555555555556,138.61111111111114,88.61111111111111,137.6388888888889,87.63888888888889,136.94444444444446C86.66666666666667,136.25000000000003,85.55555555555556,135.97222222222223,84.58333333333334,135.97222222222223C83.33333333333334,135.97222222222223,82.22222222222223,136.25000000000003,81.38888888888889,136.80555555555557C80.55555555555556,137.36111111111114,79.86111111111111,138.19444444444446,79.30555555555556,139.16666666666669C78.75,140.1388888888889,78.33333333333333,141.25000000000003,78.05555555555556,142.50000000000003C77.77777777777777,143.75000000000003,77.77777777777777,145.00000000000003,77.77777777777777,146.25000000000003C77.77777777777777,147.6388888888889,77.91666666666667,148.8888888888889,78.19444444444444,150.1388888888889C78.47222222222223,151.3888888888889,79.02777777777777,152.50000000000003,79.58333333333333,153.47222222222223C80.13888888888889,154.44444444444446,80.83333333333334,155.1388888888889,81.66666666666667,155.69444444444446C82.5,156.25000000000003,83.61111111111111,156.5277777777778,84.72222222222223,156.5277777777778C86.11111111111111,156.5277777777778,87.22222222222223,155.97222222222223,88.05555555555556,155.1388888888889C88.88888888888889,154.30555555555557,89.44444444444444,153.33333333333334,89.86111111111111,152.36111111111114M71.11111111111111,146.25000000000003C71.11111111111111,148.8888888888889,71.38888888888889,151.3888888888889,72.08333333333333,153.61111111111114C72.77777777777777,155.83333333333334,73.75,157.7777777777778,75,159.30555555555557C76.25,160.83333333333334,77.5,162.08333333333334,79.16666666666667,162.91666666666669C80.69444444444444,163.75000000000003,82.36111111111111,164.30555555555557,84.16666666666667,164.30555555555557C85.83333333333334,164.30555555555557,87.63888888888889,163.8888888888889,89.16666666666667,163.05555555555557C90.69444444444444,162.22222222222223,91.94444444444444,160.97222222222223,93.19444444444444,159.44444444444446C94.44444444444444,157.91666666666669,95.41666666666667,155.97222222222223,96.11111111111111,153.75000000000003C96.80555555555556,151.5277777777778,97.22222222222223,149.0277777777778,97.22222222222223,146.25000000000003C97.22222222222223,143.75000000000003,96.94444444444444,141.3888888888889,96.25,139.30555555555557C95.55555555555556,137.22222222222223,94.72222222222223,135.2777777777778,93.47222222222223,133.61111111111114C92.22222222222223,131.94444444444446,90.83333333333334,130.83333333333334,89.30555555555556,129.86111111111114C87.77777777777777,128.8888888888889,85.97222222222223,128.47222222222223,84.16666666666667,128.47222222222223C82.36111111111111,128.47222222222223,80.69444444444444,128.8888888888889,79.02777777777777,129.86111111111114C77.36111111111111,130.83333333333334,76.11111111111111,131.94444444444446,74.86111111111111,133.61111111111114C73.61111111111111,135.2777777777778,72.77777777777777,137.08333333333334,72.08333333333333,139.30555555555557C71.38888888888889,141.5277777777778,71.11111111111111,143.75000000000003,71.11111111111111,146.25000000000003");
console.log(pth.toString()); 

How to reduce those decimal places to n decimal places either using the linked svg parser above or any other way using js please?

Update: Thank you @Ian. Using your code, How can we use the below code to round if there is any decimals?:

 if (dec || dec === 0)
    {
      if (dec > 1) dec = 1;
      else if (dec < 0) dec = 0;
    }
    else dec = false;

    function r(num)
    {
if (dec !== false) return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
      else return num;
    }

You could use some regex like this...I think this will round as well as cut off...

var newstring = string.replace(/[0-9]*\.[0-9]*/g, function(x){ return (+x).toFixed(2)  });

Then you can apply that whereever, in a paths d attribute etc

jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM