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
Post a Comment