{"id":754,"date":"2011-11-26T18:40:34","date_gmt":"2011-11-27T00:40:34","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=754"},"modified":"2011-11-26T19:27:12","modified_gmt":"2011-11-27T01:27:12","slug":"code-of-the-day-javascript-decimal-to-fraction","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/11\/code-of-the-day-javascript-decimal-to-fraction\/","title":{"rendered":"Code of the Day: Javascript Decimal To Fraction"},"content":{"rendered":"<p>Source: <a href=\"http:\/\/stackoverflow.com\/questions\/95727\/how-to-convert-floats-to-human-readable-fractions\">mivk<\/a> from StackOverflow.com<br \/>\n<b>Code<\/b><\/p>\n<pre lang='javascript'>\r\n\/**\r\n@function dec2frac\r\n@returns string\r\n@purpose Convert a decimal to a fraction\r\n*\/\r\nfunction dec2frac(d) {\r\n    var df = 1, top = 1, bot = 1;\r\n    var limit = 1e5; \/\/Increase the limit to get more precision.\r\n  \r\n    while (df != d && limit-- > 0) {\r\n        if (df < d) {\r\n            top += 1;\r\n        }\r\n        else {\r\n            bot += 1;\r\n            top = parseInt(d * bot, 10);\r\n        }\r\n        df = top \/ bot;\r\n    }\r\n    return top + '\/' + bot;\r\n}<\/pre>\n<p><b>Demo<\/b><br \/>\n<a href=\"http:\/\/jsbin.com\/ukapoq\/edit#javascript,html\">JSBIN link<\/a><\/p>\n<p><b>Example Output<\/b><br \/>\ndec2frac ( 0.1) returns \"1\/10\"<br \/>\ndec2frac ( 0.2) returns \"1\/5\"<br \/>\ndec2frac ( 0.5) returns \"1\/2\"<br \/>\ndec2frac ( 0.9999) returns \"9999\/10000\"<br \/>\ndec2frac ( 0.75) returns \"3\/4\"<br \/>\ndec2frac ( 2.718281828459045) returns \"135915\/50000\", but this evaluates to 2.7183<br \/>\ndec2frac ( 3.141592653589793) returns \"157079\/50000\", but this evaluates to 3.14158<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: mivk from StackOverflow.com Code \/** @function dec2frac @returns string @purpose Convert a decimal to a fraction *\/ function dec2frac(d) { var df = 1, top = 1, bot = 1; var limit = 1e5; \/\/Increase the limit to get more precision. while (df != d &#038;&#038; limit&#8211; > 0) { if (df < d) &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/11\/code-of-the-day-javascript-decimal-to-fraction\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Javascript Decimal To Fraction<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[30,164,166],"class_list":["post-754","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials","tag-code-of-the-day","tag-javascript","tag-math"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=754"}],"version-history":[{"count":6,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":777,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/754\/revisions\/777"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}