{"id":10266,"date":"2022-09-23T02:02:37","date_gmt":"2022-09-22T20:32:37","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/"},"modified":"2022-09-23T02:02:37","modified_gmt":"2022-09-22T20:32:37","slug":"solved-convert-int-which-actually-decimal-to-string-1234-to-123-4","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/","title":{"rendered":"[Solved] convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;)"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-25674307\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"25674307\" data-parentid=\"25673013\" data-score=\"1\" data-position-on-page=\"3\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I was curious about the relative performance of these conversions so I did a few tests with the naive versions of a few methods. A few notes and caveats:<\/p>\n<ul>\n<li>The below conversion codes have no error checking and haven&#8217;t been tested (i.e., don&#8217;t copy\/paste the code).<\/li>\n<li>Some methods only work for positive integers.<\/li>\n<li>The methods do not have equivalent output. For example numbers 0-9 (&#8216;0.0&#8217;, &#8216;.0&#8217;, &#8216;.&#8217;) and factors of 10 (&#8217;10&#8217;, &#8217;10.&#8217;, &#8216;10.0&#8217;). You should decide exactly what output you want for these cases.<\/li>\n<li>I would start by choosing the simplest algorithm and only optimize once it is found I need to. Do you really need performance of ~100 million integers\/second? <\/li>\n<li>Unfortunately, I don&#8217;t have a C++11 compiler to test the <code>std::to_string()<\/code> versions.<\/li>\n<\/ul>\n<p>Algorithms tested are:<\/p>\n<pre><code>string toString1 (const int a)\n{\n    char buffer[32];\n    string result = itoa(a, buffer, 10);\n    char lastDigit = result[result.length() - 1];\n    result[result.length() - 1] = '.';\n    result += lastDigit;\n\n    return result;\n}\n\nvoid toString1a (string&amp; Output, const int a)\n{\n    char buffer[32];\n    Output = itoa(a, buffer, 10);\n    char lastDigit = Output[Output.length() - 1];\n    Output[Output.length() - 1] = '.';\n    Output += lastDigit;\n}\n\nstring toString2 (const int a) {\n    float f = a * 0.1f;\n    ostringstream ss;\n    ss &lt;&lt; f;\n\n    return ss.str();\n}    \n\nconst char* toString3 (const int a)\n{\n    static char s_buffer[32]; \/\/Careful!\n\n    itoa(a, s_buffer, 10);\n    size_t len = strlen(s_buffer);\n    char lastDigit = s_buffer[len-1];\n    s_buffer[len-1] = '.';\n    s_buffer[len] = lastDigit;\n    s_buffer[len+1] = '\\0';\n\n    return s_buffer;\n}    \n\nconst char* toString4 (char* pBuffer, const int a)\n{\n    itoa(a, pBuffer, 10);\n    size_t len = strlen(pBuffer);\n    char lastDigit = pBuffer[len-1];\n    pBuffer[len-1] = '.';\n    pBuffer[len] = lastDigit;\n    pBuffer[len+1] = '\\0';\n\n    return pBuffer;\n}\n\nvoid toString4a (char* pBuffer, const int a)\n{\n    itoa(a, pBuffer, 10);\n    size_t len = strlen(pBuffer);\n    char lastDigit = pBuffer[len-1];\n    pBuffer[len-1] = '.';\n    pBuffer[len] = lastDigit;\n    pBuffer[len+1] = '\\0';\n}\n\nconst char* toString5 (char* pBuffer, const int a)\n{\n    snprintf(pBuffer, 16, \"%.1f\", a\/10.0);\n    return pBuffer;\n}\n\nconst char* toString6 (char* pBuffer, const int a)\n{\n    snprintf(pBuffer, 16, \"%d.%01d\", a \/ 10, a % 10);\n    return pBuffer;\n}\n\nstring toString7 (const int a)\n{\n    ostringstream stream;\n    stream &lt;&lt; (a \/ 10) &lt;&lt; '.' &lt;&lt; (a % 10);\n    return stream.str();\n}\n\nstring toString8 (const int a)\n{\n    char buffer[16];\n    string result = _itoa(a, buffer, 10);\n    result.insert(result.length()-1, 1, '.'); \n    return result;\n}\n<\/code><\/pre>\n<p>Basic benchmarks were run on the above methods for 100 million integers (starting from 0). If performance really matters for your case please profile\/benchmark for yourself for your exact use-case.<\/p>\n<ul>\n<li>toString1 = 0.64 sec (itoa to string)<\/li>\n<li>toString1a = 0.61 sec (inplace, no return)<\/li>\n<li>toString2 = 33.4 sec (stringstream, ouch!)<\/li>\n<li>toString3 = 0.52 sec (itoa, static buffer)<\/li>\n<li>toString4 = 0.52 sec (itoa)<\/li>\n<li>toString4a = 0.52 sec (itoa, inplace)<\/li>\n<li>toString5 = 6.35 sec (snprintf, float)<\/li>\n<li>toString6 = 2.29 sec (snprintf, two ints)<\/li>\n<li>toString7 = 33.3 sec (stringstream, two ints)<\/li>\n<li>toString8 = 0.70 sec (itoa and string::insert)<\/li>\n<\/ul>\n<p>Which one is &#8220;better&#8221; really depends on what you need. Personally I would use the simplest stream case (toString7) unless I was absolutely sure I needed the performance of the other methods.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I was curious about the relative performance of these conversions so I did a few tests with the naive versions of a few methods. A few notes and caveats: The below conversion codes have no error checking and haven&#8217;t been tested (i.e., don&#8217;t copy\/paste the code). Some methods only work for positive integers. The &#8230; <a title=\"[Solved] convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;)\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\" aria-label=\"More on [Solved] convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-10266","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] convert int (which actually decimal) to string (1234 to &quot;123.4&quot;) - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] convert int (which actually decimal) to string (1234 to &quot;123.4&quot;) - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I was curious about the relative performance of these conversions so I did a few tests with the naive versions of a few methods. A few notes and caveats: The below conversion codes have no error checking and haven&#8217;t been tested (i.e., don&#8217;t copy\/paste the code). Some methods only work for positive integers. The ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-22T20:32:37+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;)\",\"datePublished\":\"2022-09-22T20:32:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\"},\"wordCount\":253,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\",\"name\":\"[Solved] convert int (which actually decimal) to string (1234 to \\\"123.4\\\") - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-22T20:32:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] convert int (which actually decimal) to string (1234 to \"123.4\") - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] convert int (which actually decimal) to string (1234 to \"123.4\") - JassWeb","og_description":"[ad_1] I was curious about the relative performance of these conversions so I did a few tests with the naive versions of a few methods. A few notes and caveats: The below conversion codes have no error checking and haven&#8217;t been tested (i.e., don&#8217;t copy\/paste the code). Some methods only work for positive integers. The ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/","og_site_name":"JassWeb","article_published_time":"2022-09-22T20:32:37+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;)","datePublished":"2022-09-22T20:32:37+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/"},"wordCount":253,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/","url":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/","name":"[Solved] convert int (which actually decimal) to string (1234 to \"123.4\") - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-22T20:32:37+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-convert-int-which-actually-decimal-to-string-1234-to-123-4\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] convert int (which actually decimal) to string (1234 to &#8220;123.4&#8221;)"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=10266"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10266\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=10266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=10266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=10266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}