0

I have this string generated with terraformer-wkt using leaflet:

POLYGON((-66.85271859169006 10.488056634656399,-66.85351252555847 10.486178802289459,-66.85342669487 10.485250431517958,-66.84864163398743))

I want to reduce the limit of decimal places upto 5 digit .

POLYGON((-66.85271 10.48805,-66.85351 10.48617,-66.85342 10.48525,-66.84864))

I saw in javascripts how reduce convert a number into a string, keeping only 5 decimals but I dont know to how use this with my string:

var num = -66.85271859169006; 
var n = num.toFixed(5); 
//result would be -66.85271
muzaffar
  • 1,706
  • 1
  • 15
  • 28
user4131013
  • 425
  • 1
  • 7
  • 14
  • 3
    Have you tried something ? – Rizier123 Jun 08 '15 at 15:03
  • I saw in javascrips how reduce Convert a number into a string, keeping only 5 decimals but I dont know how use this whith my string: var num = -66.85271859169006; var n=num.toFixed(5); result -66.85271 – user4131013 Jun 08 '15 at 15:08
  • If you don't *try* something you can't fail, so you don't know if you can do it or not. So: Just *try something*: write some code or do some research to find something or write some pseudo code, maybe you will surprise your self and you can figure it out yourself! But if you get stuck, just paste your work and we can exactly show you were you went wrong, and how to correct it – Rizier123 Jun 08 '15 at 15:10

1 Answers1

1

You can use a regular expression to search for all numbers in the string and replace them:

var str = 'POLYGON((-66.85271859169006 10.488056634656399,-66.85351252555847 10.486178802289459,-66.85342669487 10.485250431517958,-66.84864163398743))';

console.log(str.replace(/\d+\.\d+/g, function(match) {
  return Number(match).toFixed(5);
}));

See:

Related: Round to at most 2 decimal places (only if necessary)

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Felix Kling, Thank you so much for your fast reply – user4131013 Jun 08 '15 at 15:22
  • POLYGON((-66.85271859169006 10.488056634656399,-66.85351252555847 10.486178802289459,-66.85342669487 10.485250431517958,-66.84864163398743)) Is there a way to delete POLYGON(( and )) and get this -66.85271859169006 10.488056634656399,-66.85351252555847 10.486178802289459,-66.85342669487 10.485250431517958,-66.84864163398743 – user4131013 Jun 08 '15 at 15:27
  • Sure. Since these seem to be fixed substrings, just take the substring excluding those. – Felix Kling Jun 08 '15 at 15:35