Skip to main content
Stanly Hoffman
Known Participant
April 25, 2016
Answered

math.round all dimensions

  • April 25, 2016
  • 1 reply
  • 572 views

Hi, guys. I need to math.round all dimensions. For example:

"350,000 mm" -> "350 mm",

"420,001 mm" -> "420 mm"

"419.97 mm" -> "420 mm"

and so on. All in one script.

The trick is I dont know how to play with numbers and characters at the same time, I only know how to math.round numbers.

Thank you.

This topic has been closed for replies.
Correct answer CarlosCanto

there are various different ways to solve that, here's one of them

var s_number = "419.97 mm";

var delimeter = ' ';

var a_number = s_number.split(delimeter);

$.writeln(Math.round(a_number[0]) + delimeter + a_number[1]);

1 reply

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
April 26, 2016

there are various different ways to solve that, here's one of them

var s_number = "419.97 mm";

var delimeter = ' ';

var a_number = s_number.split(delimeter);

$.writeln(Math.round(a_number[0]) + delimeter + a_number[1]);

Stanly Hoffman
Known Participant
April 26, 2016

Thanks, Carlos. There is only one problem - script doesnt work with numbers with commas. I solved it by converting all commas to periods first, than converting back to commas. How to solve it any other way?

CarlosCanto
Community Expert
Community Expert
April 27, 2016

convert commas to periods (if any) only once, since you're getting rid of the decimals

this version works with both commas & periods

var s_number = "419,97 mm";

var delimeter = ' ';

var a_number = s_number.split(delimeter);

$.writeln(Math.round(a_number[0].replace(',','.')) + delimeter + a_number[1]);