JavaScript regex - Format decimal with trailing zero and string -


i have different format of prices, needs displayed below:

1.09-----> 1.09 1.00----> 1 1.00 lb --->1lb 1.09 lb---->1.09lb 

i need in building regex javascript display above prices in specified formats.

parse , format numeric portion using parsefloat , number.tostring. add special case handle lbs:

function formatprice(price) {     return parsefloat(price) + (price.match(/ .+$/) ? price.match(/ (.+)$/)[1] : ""); }  console.log(formatprice("1.00"));    // 1 console.log(formatprice("1.09"));    // 1.09 console.log(formatprice("1.09000")); // 1.09 console.log(formatprice("1.00 lb")); // 1lb console.log(formatprice("1.09 lb")); // 1.09lb console.log(formatprice("1.09 kg")); // 1.09kg 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -