<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ไอเอินบล๊อก &#187; sprintf</title>
	<atom:link href="http://www.iearn.in.th/tag/sprintf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iearn.in.th</link>
	<description>Just Play and Just Knowledge</description>
	<lastBuildDate>Fri, 05 Aug 2011 15:10:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Number format in Rails</title>
		<link>http://www.iearn.in.th/2009/05/17/number-format-in-rails/</link>
		<comments>http://www.iearn.in.th/2009/05/17/number-format-in-rails/#comments</comments>
		<pubDate>Sun, 17 May 2009 09:28:49 +0000</pubDate>
		<dc:creator>nolifelover</dc:creator>
				<category><![CDATA[RoR]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails plugins]]></category>
		<category><![CDATA[rails tutorails]]></category>
		<category><![CDATA[sprintf]]></category>
		<category><![CDATA[start with rails]]></category>

		<guid isPermaLink="false">http://www.iearn.in.th/?p=470</guid>
		<description><![CDATA[วันนี้เอาทริปอาจจะไม่เด็ดเท่าไรเพราะ programmer ทั่วไปคงรู้กันแล้ว แต่สำหรับมือใหม่คงต้องใช่ครับ สำหรับใครที่ต้องการจะแสดงผลเป็น 0001 แต่มีตัวเลขแค่ 1 เราสามารถใช้งาน rails sprintf ซึ่งมีให้ใช้งานอยู่แล้ว 1 2 &#160; sprintf&#40;'%04i', 1&#41; #result 0001 แล้วถ้าเกิดผมต้องการใช้ตัวเลขมี prefix และ auto generate อัตโนมัติเมื่อสร้างขึ้นมาจะทำอย่างไรลองมาดูกันครับ สำหรับเลขชุดที่ผมต้องการตอ้งขึ้นต้นด้วย TX01H และมีลำดับตามเป็นเลข 4 ตัวเรียกกันไปครับคือ 0001&#8230; มาดูโค้ดกันครับ 1 2 3 4 &#160; @last_txaction = Txaction.find&#40;:first,:conditions=&#62;&#91;&#34;code like ?&#34;,&#34;TX01H%&#34;&#93;,:order =&#62; &#34;code desc&#34;&#41; #find last code result =&#62;TX01Hxxxx code = @last_txaction ? @last_txaction.code.gsub&#40;&#34;TX01H&#34;,&#34;&#34;&#41;.to_i+1:1 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.iearn.in.th/wp-content/uploads/2009/05/rails-number-format.jpg"><img src="http://www.iearn.in.th/wp-content/uploads/2009/05/rails-number-format-300x118.jpg" alt="rails-number-format" title="rails-number-format" width="300" height="118" class="aligncenter size-medium wp-image-471" /></a></p>
<p>วันนี้เอาทริปอาจจะไม่เด็ดเท่าไรเพราะ programmer ทั่วไปคงรู้กันแล้ว แต่สำหรับมือใหม่คงต้องใช่ครับ สำหรับใครที่ต้องการจะแสดงผลเป็น 0001 แต่มีตัวเลขแค่ 1 เราสามารถใช้งาน rails sprintf ซึ่งมีให้ใช้งานอยู่แล้ว<br />
<span id="more-470"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
  <span style="color:#CC0066; font-weight:bold;">sprintf</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'%04i'</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#result 0001</span></pre></td></tr></table></div>

<p>แล้วถ้าเกิดผมต้องการใช้ตัวเลขมี prefix และ auto generate อัตโนมัติเมื่อสร้างขึ้นมาจะทำอย่างไรลองมาดูกันครับ สำหรับเลขชุดที่ผมต้องการตอ้งขึ้นต้นด้วย TX01H และมีลำดับตามเป็นเลข 4 ตัวเรียกกันไปครับคือ 0001&#8230; มาดูโค้ดกันครับ</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
  <span style="color:#0066ff; font-weight:bold;">@last_txaction</span> = Txaction.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>,:conditions<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;code like ?&quot;</span>,<span style="color:#996600;">&quot;TX01H%&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>,:order <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;code desc&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#find last code result =&gt;TX01Hxxxx</span>
  code = <span style="color:#0066ff; font-weight:bold;">@last_txaction</span> ? <span style="color:#0066ff; font-weight:bold;">@last_txaction</span>.<span style="color:#9900CC;">code</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;TX01H&quot;</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span>:<span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;">#if not found start code from 1</span>
  <span style="color:#0066ff; font-weight:bold;">@txaction</span>.<span style="color:#9900CC;">code</span> = <span style="color:#996600;">&quot;TX01H&quot;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#CC0066; font-weight:bold;">sprintf</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'%04i'</span>, code<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>เป็นอันว่าเรียบร้อยครับ</p>

<p class="sayac_bilgi">12 views</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iearn.in.th/2009/05/17/number-format-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

