{"id":67483,"date":"2006-04-21T13:41:00","date_gmt":"2006-04-21T13:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/21\/how-can-i-reformat-numbers-using-a-script\/"},"modified":"2006-04-21T13:41:00","modified_gmt":"2006-04-21T13:41:00","slug":"how-can-i-reformat-numbers-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-reformat-numbers-using-a-script\/","title":{"rendered":"How Can I Reformat Numbers Using a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! I have a text file containing latitudes and longitudes. The numbers look like this: 36000000. I need to format these numbers so they look like this: 36.000000. How can I reformat numbers using a script?<BR><BR>&#8212; BE<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, BE. You know, if you have a question about numbers then you came to the right place. After all, one of the Scripting Guys is the proud owner of a degree in advanced mathematics. And the other Scripting Guys \u2026 well, never mind about the other Scripting Guys. Did we mention that one of the Scripting Guys is the proud owner of a degree in advanced mathematics?<\/P>\n<P>Fortunately, you don\u2019t need a degree in advanced mathematics to solve this problem. (And fortunately we don\u2019t need one either, or this might be a <I>really<\/I> short column today.) As you noted, BE, you have a couple of text files containing latitude and longitude entries similar to this:<\/P><PRE class=\"codeSample\">36000000\n-076000000\n92000000\n105000000\n<\/PRE>\n<P>What you want to do is open the text file, reformat the numbers, and then save the reformatted text file. When all is said and done you\u2019d like your text file to look like this:<\/P><PRE class=\"codeSample\">36.000000\n-76.000000\n92.000000\n105.000000\n<\/PRE>\n<P>Can we accomplish this feat using a script? Piece of cake:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\test.txt&#8221;, ForReading)<\/p>\n<p>Do Until objFile.AtEndOfStream\n    intLine = objFile.Readline\n    intLine = intLine\/ 1000000\n    intLine = FormatNumber(intLine, 6)\n    strText = strText &amp; intLine &amp; vbCrLf\nLoop<\/p>\n<p>objFile.Close\nSet objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\test.txt&#8221;, ForWriting)<\/p>\n<p>objFile.Write strText\nobjFile.Close\n<\/PRE>\n<P>And don\u2019t worry: we\u2019ll explain how this all works. After all, that\u2019s what we\u2019re here for, right?<\/P>\n<P>As you can see, the script starts out simple enough. We begin by defining a pair of constants &#8211; ForReading and ForWriting &#8211; which we\u2019ll use when working with the text file itself. We create an instance of the <B>FileSystemObject<\/B> and then use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Test.txt for reading. (As you might well know, you can open a text file for reading or you can open a text file for writing, but you can\u2019t perform both operations at the same time.)<\/P>\n<P>That brings us to this block of code:<\/P><PRE class=\"codeSample\">Do Until objFile.AtEndOfStream\n    intLine = objFile.Readline\n    intLine = intLine\/ 1000000\n    intLine = FormatNumber(intLine, 6)\n    strText = strText &amp; intLine &amp; vbCrLf\nLoop\n<\/PRE>\n<P>This is where all the excitement takes place. To begin with, we set up a Do Until loop to read through the file line-by-line, then continue reading until we\u2019ve read every last line in the file (that is, when the <B>AtEndOfStream<\/B> property is True). Inside that loop we use the <B>ReadLine<\/B> method to read the first line of the file and store that value (in our sample file, 36000000) in a variable named intLine.<\/P>\n<P>The next step is easy. We want to take the number 36000000 and turn it into 36.000000. How do we do that? For starters, we divide the value by 1000000:<\/P><PRE class=\"codeSample\">intLine = intLine\/ 1000000\n<\/PRE>\n<P>That\u2019s going to give us &#8211; in this case &#8211; the value 36. To display that value with 6 decimal points all we have to do is call on the <B>FormatNumber<\/B> function:<\/P><PRE class=\"codeSample\">intLine = FormatNumber(intLine, 6)\n<\/PRE>\n<P>All we\u2019re doing here is assigning a new value to the variable intLine. What\u2019s that new value going to be? Well, it\u2019s going to be the result of running the existing value of intLine (36) through the FormatNumber function, asking FormatNumber to display the result using 6 decimal places. (That\u2019s what the parameter <B>6<\/B> is for: it indicates the number of decimal places we want displayed.) The end result: after turning 36000000 into 36, we\u2019ve now turned 36 into 36.000000. Which is the very thing we set out to do in the first place.<\/P>\n<P>After converting the first line (first number) in the text file we then add the new value plus a carriage return-linefeed (vbCrLf) to a variable named strText:<\/P><PRE class=\"codeSample\">strText = strText &amp; intLine &amp; vbCrLf\n<\/PRE>\n<P>As you probably figured out, strText is simply a variable that keeps track of our reformatted numbers. After we\u2019ve read through all the lines in the text file the value of strText will look like this:<\/P><PRE class=\"codeSample\">36.000000\n-76.000000\n92.000000\n105.000000\n<\/PRE>\n<P>If you\u2019re thinking, \u201cSay, that looks a lot like the way we want the numbers in the text file to be formatted,\u201d well, give yourself a gold star: this looks <I>exactly<\/I> like the way we want the numbers in the text file to be formatted.<\/P>\n<P>Because of that we can achieve our end goal simply by saving the value of strText to the text file. To do that we first close the file C:\\Scripts\\Test.txt. Why? Remember, we opened the file for reading; in order to write to it, we have to close the file and then reopen it for writing. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">objFile.Close\nSet objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\test.txt&#8221;, ForWriting)\n<\/PRE>\n<P>We use the <B>Write<\/B> method to write the value of strText to the text file and then close the file one last time. The net result: our text file now looks like this:<\/P><PRE class=\"codeSample\">36.000000\n-76.000000\n92.000000\n105.000000\n<\/PRE>\n<P>Not bad for a bunch of Scripting Guys with limited math skills, is it? <\/P>\n<TABLE id=\"EUE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Despite our self-deprecation we should point out that one of the Scripting Guys completed his entire income tax return <I>without using a calculator<\/I>! Granted, you don\u2019t really <I>need<\/I> a calculator when working with tiny numbers like his salary, but, still, we thought it was impressive nonetheless. <I>(Editor\u2019s Note: Before the IRS decides to audit all of us, we\u2019ll save them the trouble and point out that it was Greg.)<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a text file containing latitudes and longitudes. The numbers look like this: 36000000. I need to format these numbers so they look like this: 36.000000. How can I reformat numbers using a script?&#8212; BE Hey, BE. You know, if you have a question about numbers then you came to the [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,14,5],"class_list":["post-67483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a text file containing latitudes and longitudes. The numbers look like this: 36000000. I need to format these numbers so they look like this: 36.000000. How can I reformat numbers using a script?&#8212; BE Hey, BE. You know, if you have a question about numbers then you came to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67483\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}