{"id":188,"date":"2017-10-18T08:33:27","date_gmt":"2017-10-18T10:33:27","guid":{"rendered":"http:\/\/hackerzone.com.br\/blog\/?p=188"},"modified":"2017-10-18T08:33:27","modified_gmt":"2017-10-18T10:33:27","slug":"trabalhar-com-valor-monetario-em-javascript","status":"publish","type":"post","link":"http:\/\/hackerzone.com.br\/blog\/trabalhar-com-valor-monetario-em-javascript\/","title":{"rendered":"Trabalhar com valor monet\u00e1rio em JavaScript"},"content":{"rendered":"<p>Quem esta come\u00e7ando a trabalhar com valores monet\u00e1rios em JavaScript sofre muito, portanto vou deixar aqui alguns m\u00e9todos simples e eficientes de realizar c\u00e1lculos e formata\u00e7\u00e3o dos valores.<\/p>\n<p>Semelhante a fun\u00e7\u00e3o round do PHP que arredonda valores em um certo n\u00famero de casas decimais.<br \/>\n<strong>roundNumber<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction roundNumber (rnum) {\r\n\r\n   return Math.round(rnum*Math.pow(10,2))\/Math.pow(10,2);\r\n\r\n}\r\n<\/pre>\n<p><strong>float2moeda<\/strong><br \/>\nA partir de um valor float ela retorna o valor formatado com separador de milhar e v\u00edrgula nos centavos.<br \/>\n<strong>float2moeda<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction float2moeda(num) {\r\n   x = 0;\r\n\r\n   if(num&lt;0) {\r\n      num = Math.abs(num);\r\n      x = 1;\r\n   }\r\n   if(isNaN(num)) num = &quot;0&quot;;\r\n      cents = Math.floor((num*100+0.5)%100);\r\n\r\n   num = Math.floor((num*100+0.5)\/100).toString();\r\n\r\n   if(cents &lt; 10) cents = &quot;0&quot; + cents;\r\n      for (var i = 0; i &lt; Math.floor((num.length-(1+i))\/3); i++)\r\n         num = num.substring(0,num.length-(4*i+3))+'.'\r\n               +num.substring(num.length-(4*i+3));\r\n   ret = num + ',' + cents;\r\n   if (x == 1) ret = ' - ' + ret;return ret;\r\n\r\n}\r\n<\/pre>\n<p>Ex: Se voc\u00ea passar o valor 540 nesta fun\u00e7\u00e3o ele retornar\u00e1 540,00<br \/>\nSe voc\u00ea passar o valor 540.12 ele retornar\u00e1 540,12 particularmente achei essa fun\u00e7\u00e3o fant\u00e1stica.<\/p>\n<p><strong>moeda2float<\/strong><\/p>\n<p>Pega um valor formatado com virgula e separador de milha e o transforma em float.<br \/>\n<strong>moeda2float<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction moeda2float(moeda){\r\n\r\n   moeda = moeda.replace(&quot;.&quot;,&quot;&quot;);\r\n\r\n   moeda = moeda.replace(&quot;,&quot;,&quot;.&quot;);\r\n\r\n   return parseFloat(moeda);\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Quem esta come\u00e7ando a trabalhar com valores monet\u00e1rios em JavaScript sofre muito, portanto vou deixar aqui alguns m\u00e9todos simples e eficientes de realizar c\u00e1lculos e formata\u00e7\u00e3o dos valores. Semelhante a fun\u00e7\u00e3o round do PHP que arredonda valores em um certo n\u00famero de casas decimais. roundNumber float2moeda A partir de um valor float ela retorna o [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[32,89],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-valor-monetario","cat-29-id"],"_links":{"self":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":1,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":189,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions\/189"}],"wp:attachment":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}