Javascript

var stroka = "48,500 52 68,320"; 

As a result, get 168,820

    1 answer 1

     var stroka = "48,500 52 68,320"; var summ = 0.0; stroka.split(' ').map(item => { summ += parseFloat(item.replace(',', '.')); return item; }) // устанавливаем float с 3мя знаками после запятой result = summ.toFixed(3) + ""; result = result.replace('.', ',') 

    A new feature standard called "arrow functions" from ES6 has been used, introduced already in Chrome, Firefox. Not supported yet by Opera and Safari

    An example through a standard function:

     var stroka = "48,500 52 68,320"; var summ = 0.0; stroka.split(' ').map(function(item) { summ += parseFloat(item.replace(',', '.')); return item; }) // устанавливаем float с 3мя знаками после запятой result = summ.toFixed(3) + ""; result = result.replace('.', ',') 
    • Thanks, but the result was not 168.82 format, but 168.820 is it possible? - sashatexb
    • (item) => change to function (item) - Dmitry Chistik
    • @ Dmitriy Chistik read the documentation if it’s interesting how the code is written, this is ES6, I changed the post with a description of it - Vasily Barbashev
    • Vasily Barbashev, I'm just a supporter of maximum adaptation for browsers. I have 3 browsers Your code did not work. About the standard vkurse. - Dmitry Chistik
    • @sashatexb I have a question, if these are integers, only separated by a comma (each three numbers), then the algorithm must be made different, because otherwise it turns out that we add non-integer numbers such as 48.5 + 52 + 68.32. If we add integers, we will have 168820, and only then we can break the line and divide the numbers with a comma, if necessary, I can do - Vasily Barbashev