<?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"
	>

<channel>
	<title>Arquiva-me</title>
	<atom:link href="http://manifesto42.com/42/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://manifesto42.com/42</link>
	<description>Flex, PHP, Mysql, Flash, doubts and solutions. Kinda like IKEA, but about programming. And smaller. And not swedish.</description>
	<pubDate>Fri, 23 Jul 2010 12:05:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>update table with values from other table</title>
		<link>http://manifesto42.com/42/?p=64</link>
		<comments>http://manifesto42.com/42/?p=64#comments</comments>
		<pubDate>Wed, 17 Jun 2009 19:16:01 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[join]]></category>

		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=64</guid>
		<description><![CDATA[I ran into a problem where i had to update two values on a table. One was fixed, but the other depended on a value stored on a second table. This is how its done.


TableA
id_cli   test_cut
1          15

2          56
TableB
id    num_of        job    test_cut    id_client
1     20090464    34      0               2
2     20081012    200    0               1
I needed to update the [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a problem where i had to update two values on a table. One was fixed, but the other depended on a value stored on a second table. This is how its done.</p>
<p><span style="color: #33cccc;"><br />
</span></p>
<p><strong><span style="color: #3366ff;">TableA</span></strong></p>
<p><span style="color: #3366ff;">id_cli   test_cut</span></p>
<p><span style="color: #3366ff;">1          15<br />
</span></p>
<p><span style="color: #3366ff;">2          56</span></p>
<p><strong><span style="color: #3366ff;">TableB</span></strong></p>
<p><span style="color: #3366ff;">id    num_of        job    test_cut    id_client</span></p>
<p><span style="color: #3366ff;">1     20090464    34      0               2</span></p>
<p><span style="color: #3366ff;">2     20081012    200    0               1</span></p>
<p>I needed to update the value of the fields &#8220;job&#8221; and &#8220;test_cut&#8221; on Table B, but the value i wanted to put on &#8220;test_cut&#8221; needs to come from Table A, linked by field &#8220;id_cli&#8221;. Confusing?</p>
<p><span style="color: #0000ff;">update TableB Tb,TableA Ta set Tb.job_meio=&#8217;550&#8242;,Tb.test_cut=Ta.test_cut where Tb.num_of=&#8217;20090494&#8242; and Ta.id_cli=Tb.id_cli</span></p>
<p>This update joins the two tables by the field id_cli, fetches the test_cut on TableA and updates fields test_cut and job_meio on TableB</p>
<p>This will turn <span style="text-decoration: underline;">TableB</span> into:</p>
<p>id    num_of        job      test_cut    id_client</p>
<p>1     20090464    <span style="color: #ff0000;"><strong>550</strong></span> <span style="color: #ff0000;"><strong>56</strong></span> 2</p>
<p>2     20081012    200     0               1</p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=64</wfw:commentRss>
		</item>
		<item>
		<title>Air project doesn&#8217;t run. No error.</title>
		<link>http://manifesto42.com/42/?p=58</link>
		<comments>http://manifesto42.com/42/?p=58#comments</comments>
		<pubDate>Fri, 08 May 2009 13:04:32 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[Flex]]></category>

		<category><![CDATA[Flex Air]]></category>

		<category><![CDATA[air]]></category>

		<category><![CDATA[no error]]></category>

		<category><![CDATA[problem]]></category>

		<category><![CDATA[run]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=58</guid>
		<description><![CDATA[Didn&#8217;t quite know what to use as a title, because the problem sounds a bit too generic. Anyway, on to the problem/solution.
The problem was that i was trying to convert an already built/in progress Flex project to Air and it was giving me some grief, since it simply didnt run. No error, nothing. The only [...]]]></description>
			<content:encoded><![CDATA[<p>Didn&#8217;t quite know what to use as a title, because the problem sounds a bit too generic. Anyway, on to the problem/solution.</p>
<p>The problem was that i was trying to convert an already built/in progress Flex project to Air and it was giving me some grief, since it simply didnt run. No error, nothing. The only message i got was if i tried to debug it, where i would get:</p>
<p><span style="color: #3366ff;">Process terminated without establishing connection to debugger.</span></p>
<p><span style="color: #3366ff;">Command:</span></p>
<p><span style="color: #3366ff;">C:\win2kapp\FlexBuilder3\sdks\3.3.0\bin\adl.exe D:\AppServ\www\workspace\tg_preimpressao_air_2009.05.08\bin-debug\main_preimpressao-app.xml D:\AppServ\www\workspace\tg_preimpressao_air_2009.05.08\bin-debug </span></p>
<p><span style="color: #3366ff;">Output from command:</span></p>
<p><span style="color: #3366ff;">invocation forwarded to primary instance</span></p>
<p>From some googling around, i found the error to be because the Air version on my Flex project was wrong to the one of the new Air project.</p>
<p>I had to go to the &lt;project name&gt;-app.mxml file in the src dir and change the second line from:</p>
<p><span style="color: #3366ff;">&lt;application xmlns=&#8221;http://ns.adobe.com/air/application/<span style="color: #ff0000;">1.0</span>&#8220;&gt;</span></p>
<p>to</p>
<p><span style="color: #3366ff;">&lt;application xmlns=&#8221;http://ns.adobe.com/air/application/<span style="color: #ff0000;">1.5</span>&#8220;&gt;</span></p>
<p>Notice the version diference.</p>
<p>Stir. Compile. Run. Smile.</p>
<p>NOTE: Alternatively, you can create a new Air project and open its &lt;project name&gt;-app.mxml to see the version number of your Air.</p>
<p>This solution was found somewhere on the net, cant really recall where, and was documented on a bug track in adobe.</p>
<p>EDIT: This problem happened again today and this time it was nothing of the above. It seems it can happen when, by some misfortune (i dont know why), adl.exe is still running when we try to run our Air project. I solved it by going to task manager and terminating the process. Everything compiled and ran normally from then on.</p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=58</wfw:commentRss>
		</item>
		<item>
		<title>Compare two tables/fields</title>
		<link>http://manifesto42.com/42/?p=43</link>
		<comments>http://manifesto42.com/42/?p=43#comments</comments>
		<pubDate>Mon, 30 Mar 2009 16:14:07 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[compare]]></category>

		<category><![CDATA[field]]></category>

		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=43</guid>
		<description><![CDATA[Found this somewhere on google.
If we need to compare two tables, this query will list the missing records on one of them (on table2):
SELECT t1.id
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL
Then you do it the other way around to check the mising records on the other table (table1).
To [...]]]></description>
			<content:encoded><![CDATA[<p>Found this somewhere on google.</p>
<p>If we need to compare two tables, this query will list the missing records on one of them (on table2):</p>
<p><code>SELECT t1.id<br />
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id<br />
WHERE t2.id IS NULL</code></p>
<p>Then you do it the other way around to check the mising records on the other table (table1).</p>
<p>To check for different values in identical fields:</p>
<p><code>SELECT t1.id<br />
FROM table1 t1<br />
INNER JOIN table2 t2 ON t1.id = t2.id<br />
WHERE t1.field1 &lt;&gt; t2.field1<br />
OR t1.field2 &lt;&gt; t2.field2</code><br />
etc&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=43</wfw:commentRss>
		</item>
		<item>
		<title>Mysql command line</title>
		<link>http://manifesto42.com/42/?p=34</link>
		<comments>http://manifesto42.com/42/?p=34#comments</comments>
		<pubDate>Mon, 30 Mar 2009 10:11:48 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[command]]></category>

		<category><![CDATA[dump]]></category>

		<category><![CDATA[help]]></category>

		<category><![CDATA[restore]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=34</guid>
		<description><![CDATA[This is a simple reminder of the mysql command line tool.
In windows, open a command line, navigate to mysql binary folder (if you have it on your PATH, you can skip the navigation part), and use:
To login:
mysql -u #username# -p#password#
(if you dont add the #password#.. it will ask you for one. Also, note that the [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple reminder of the mysql command line tool.</p>
<p>In windows, open a command line, navigate to mysql binary folder (if you have it on your PATH, you can skip the navigation part), and use:</p>
<p><strong>To login:</strong></p>
<p><code>mysql -u #username# -p#password#</code></p>
<p>(if you dont add the #password#.. it will ask you for one. Also, note that the password doesnt have a space between the switch)</p>
<p><strong>To show all the databases:</strong></p>
<p><code>mysql&gt; show databases;</code></p>
<p><strong>To switch a database:</strong></p>
<p><code>mysql&gt; use dbname;</code></p>
<p><strong>To list the tables in the selected db:</strong></p>
<p><code>mysql&gt; show tables;</code></p>
<p><strong>To see database&#8217;s field formats:</strong></p>
<p><code>mysql&gt; describe table_name;</code></p>
<p><strong>To show columns and column information of table_name:</strong></p>
<p><code>mysql&gt; show columns from table_name;</code></p>
<p><strong>All mysql commands apply:</strong></p>
<p><code>mysql&gt; select * from table_name where field_name="blahblah";</code></p>
<p>NOTE - Always end the command with ;</p>
<p><strong>MYSQL Dump/Restore</strong></p>
<p>On the binary folder, theres another command - mysqldump - that makes, well, dumps.</p>
<p><strong>To make a dump of all the databases on the server:</strong></p>
<p><code>mysqldump -u #username# -p#password# -A &gt; dumpfile.sql</code></p>
<p><strong>Dump only certain databases:</strong></p>
<p><code>mysqldump -u #username# -p#password# db_1 db_2 db_3 &gt; dumpfile.sql</code></p>
<p><strong>Restore a database dump:</strong></p>
<p><code>mysql --verbose -user=#username# --password=#password# db_1 &lt; dumpfile.sql</code></p>
<p>NOTE - note the difference in the mysqdump and mysql when adding the switch for username and password, you can use either &#8220;-u #username#&#8221; or &#8220;&#8211;username=#username#&#8221;.</p>
<p><strong>Command line help:</strong></p>
<p><strong>General help:</strong></p>
<p><code>mysql&gt; help</code></p>
<p><strong>See the help contents:</strong></p>
<p><code>mysql&gt; help contents</code></p>
<p><strong>To see help on a specific command/category type:</strong></p>
<p><code>mysql&gt; help _command_or_category_</code></p>
<p><strong>Some examples:</strong></p>
<p><code>mysql&gt; help select<br />
mysql&gt; help administration<br />
mysql&gt; help me because i dont know anything about mysql and want to learn everything in 3 days (ok, doesnt work, but you can still try it for the error message)</code></p>
<p>Links to resources:</p>
<p><a href="http://www.pantz.org/software/mysql/mysqlcommands.html" target="_blank">MYSQL commands</a></p>
<p><a href="http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html" target="_blank">mysqldump</a></p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=34</wfw:commentRss>
		</item>
		<item>
		<title>Find string in string</title>
		<link>http://manifesto42.com/42/?p=26</link>
		<comments>http://manifesto42.com/42/?p=26#comments</comments>
		<pubDate>Fri, 13 Mar 2009 14:54:42 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[search]]></category>

		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=26</guid>
		<description><![CDATA[Like other people i&#8217;ve seen complaining about in the net, i keep forgetting how to do this.
There are several ways of doing this, but here&#8217;s a simple one:
$haystack = "Once upon a time, on a far away kingdom...";
$needle = "kingdom";
if(strstr($haystack,$needle)){
    echo "Word found.";
}else{
    echo "Couldn't find the word.";
}
The function [...]]]></description>
			<content:encoded><![CDATA[<p>Like other people i&#8217;ve seen complaining about in the net, i keep forgetting how to do this.</p>
<p>There are several ways of doing this, but here&#8217;s a simple one:</p>
<pre>$haystack = "Once upon a time, on a far away kingdom...";</pre>
<pre>$needle = "kingdom";</pre>
<pre>if(strstr($haystack,$needle)){
    echo "Word found.";
}else{
    echo "Couldn't find the word.";
}</pre>
<p>The function strstr($haystack,$needle) is case sensitive in its search.</p>
<p>If it doesnt find the $needle, it will return false, otherwise returns the string from the first occurence of the needle to the end of the haystack text.</p>
<p>There&#8217;s also the function stristr($haystack,$needle) does case insensitive search.</p>
<p>Here are links to the manual:</p>
<p><a href="http://pt.php.net/manual/en/function.strstr.php" target="_blank">strstr()</a><br />
<a href="http://pt.php.net/manual/en/function.stristr.php" target="_blank">stristr()</a></p>
<p>Other alternatives (regEx, etc):</p>
<p>strrchr() - Find the last occurrence of a character in a string<br />
substr() - Return part of a string<br />
preg_match() - Perform a regular expression match</p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=26</wfw:commentRss>
		</item>
		<item>
		<title>update table with condition from different table</title>
		<link>http://manifesto42.com/42/?p=23</link>
		<comments>http://manifesto42.com/42/?p=23#comments</comments>
		<pubDate>Thu, 26 Feb 2009 11:59:19 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[join]]></category>

		<category><![CDATA[multiple]]></category>

		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=23</guid>
		<description><![CDATA[If we want to make an update to a table, but have to condition that update with values from a second one, we do something like this:
TABLE_1
id_t1    name    fee
1         john      100
2      [...]]]></description>
			<content:encoded><![CDATA[<p>If we want to make an update to a table, but have to condition that update with values from a second one, we do something like this:</p>
<p>TABLE_1<br />
id_t1    name    fee<br />
1         john      100<br />
2         rita       100<br />
3         peter     100</p>
<p>TABLE_2<br />
id_t2      id_t1     type<br />
1           1          single<br />
2           2          single<br />
3           3          married</p>
<p>If we want to update the field &#8220;fee&#8221; from TABLE_1, for example, to make single people pay more than married people (i know, stupid example, but bear with me), we can do this:</p>
<p><code>update TABLE_1 t1,TABLE_2 t2 set t1.fee=200 where t1.id_t1=t2.id_t1 and t2.type='single'<br />
</code></p>
<p>this is going to update all the rows in TABLE_1 that belong to &#8220;single&#8221; people, resulting in:</p>
<p>TABLE_1<br />
id_t1    name      fee<br />
1         john        200<br />
2         rita         200<br />
3         peter      100</p>
<p>The update is an inner join with the comma operator.</p>
<p>Heres a link to the MySQL manual page on the update command as a reference. Has some nice examples:</p>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/update.html" target=_blank>http://dev.mysql.com/doc/refman/5.0/en/update.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=23</wfw:commentRss>
		</item>
		<item>
		<title>Format/convert decimal to hexadecimal (or other numeral system)</title>
		<link>http://manifesto42.com/42/?p=19</link>
		<comments>http://manifesto42.com/42/?p=19#comments</comments>
		<pubDate>Mon, 23 Feb 2009 12:37:21 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[Flex]]></category>

		<category><![CDATA[convert]]></category>

		<category><![CDATA[decimal]]></category>

		<category><![CDATA[format]]></category>

		<category><![CDATA[hexadecimal]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=19</guid>
		<description><![CDATA[My problem was to convert some calculations from decimal to hexadecimal to be used in a canvas background color. Specifically, convert from CMYK, from a pantone list, to RGB and apply the result to a canvas.
In a different post i will describe the convertion, might be useful for someone.
Flex has a way of converting to [...]]]></description>
			<content:encoded><![CDATA[<p>My problem was to convert some calculations from decimal to hexadecimal to be used in a canvas background color. Specifically, convert from CMYK, from a pantone list, to RGB and apply the result to a canvas.</p>
<p>In a different post i will describe the convertion, might be useful for someone.</p>
<p>Flex has a way of converting to several numeral systems. For example, from the manual:</p>
<p><code>var myuint:uint = 9;<br />
trace(myuint.toString(2)); // output: 1001 - binary system<br />
trace(myuint.toString(8)); // output: 11 - octal system</code></p>
<p>And to create hexadecimal values, to be used in uint format:</p>
<p><code>var r:uint = 250;<br />
var g:uint = 128;<br />
var b:uint = 114;<br />
var rgb:String = "0x" + r.toString(16) + g.toString(16) + b.toString(16);<br />
trace(rgb); // 0xfa8072</code></p>
<p>All seems well, except when the result in hexadecimal is just one caracter long, so be sure to check and pad it with a zero.<br />
A simple example:</p>
<p><code>var r:uint = 8<br />
var rs:String = (r.toString(16).length == 1) ? "0"+r.toString(16) : r.toString(16);</code></p>
<p>Breaking it down:</p>
<p>r.toString(16) &#8211;&gt; converts the var r to hexadecimal, which equals &#8220;8&#8243;.</p>
<p>I then check the length of the result and, in case it equals 1, add a &#8220;0&#8243; to the beginning of the string to pad it.</p>
<p>UPDATE/FIX: As pointed out by Joe (comment below), r.toString(16) converts the decimal number to string representation of a hexadecimal number.</p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=19</wfw:commentRss>
		</item>
		<item>
		<title>Automatic scroll to the bottom of a TextArea</title>
		<link>http://manifesto42.com/42/?p=16</link>
		<comments>http://manifesto42.com/42/?p=16#comments</comments>
		<pubDate>Wed, 18 Feb 2009 11:04:41 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[Flex]]></category>

		<category><![CDATA[scroll]]></category>

		<category><![CDATA[TextArea]]></category>

		<category><![CDATA[valueCommit]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=16</guid>
		<description><![CDATA[On my current project, i have a debug TextArea that continuously lists some var values and traces. This way i can keep track of whats going on without stopping execution to debug in Flex.
As the list fills, the Text starts going off the screen, downwards. I needed to keep the scroll on the TextArea always [...]]]></description>
			<content:encoded><![CDATA[<p>On my current project, i have a debug TextArea that continuously lists some var values and traces. This way i can keep track of whats going on without stopping execution to debug in Flex.</p>
<p>As the list fills, the Text starts going off the screen, downwards. I needed to keep the scroll on the TextArea always on the last piece of text. I googled and i found a solution by Joan Lafferty, from the blog &#8220;flex butterflies and bugs&#8221;, so all credit goes to where its due. I leave a link to the original post at the end of this one.</p>
<p>Anyway, the solution is as follows:</p>
<p>&lt;mx:TextArea<br />
<span style="color: #0000ff;">valueCommit=&#8221;txtl.verticalScrollPosition=txtl.maxVerticalScrollPosition&#8221;</span><br />
id=&#8221;txtl&#8221;<br />
x=&#8221;202&#8243;<br />
y=&#8221;10&#8243;<br />
height=&#8221;206&#8243;<br />
width=&#8221;122&#8243;<br />
text=&#8221;{log}&#8221;<br />
editable=&#8221;false&#8221;<br />
enabled=&#8221;true&#8221;<br />
wordWrap=&#8221;true&#8221;<br />
textAlign=&#8221;right&#8221;/&gt;</p>
<p>Save for the usual dimension/position properties, what you should look at is the <span style="color: #0000ff;">valueCommit</span> method, which will simply keep the scroll at the last bit of content in the TextArea.</p>
<p>Joan&#8217;s post is far better explained than this, and has an example. I am just posting this here as a reminder to myself and to other forgetful geeks out there.</p>
<p>Heres the link to Joan&#8217;s blog post about this subject:</p>
<p><a href="http://butterfliesandbugs.wordpress.com/2007/08/20/scrolling-to-the-bottom-of-a-container-or-textarea-automatically/" target="_blank">http://butterfliesandbugs.wordpress.com/2007/08/20/scrolling-to-the-bottom-of-a-container-or-textarea-automatically/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=16</wfw:commentRss>
		</item>
		<item>
		<title>insert into table from a second table</title>
		<link>http://manifesto42.com/42/?p=10</link>
		<comments>http://manifesto42.com/42/?p=10#comments</comments>
		<pubDate>Thu, 04 Dec 2008 18:47:44 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[insert]]></category>

		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=10</guid>
		<description><![CDATA[Its simple but i thought of leaving it here if i forget (which is usually the case) how to do it again.
If you want to insert the records from tableA into tableB, you do something like this:
INSERT INTO tableA SELECT * FROM tableB
You have to make sure all fields are the same on both tables [...]]]></description>
			<content:encoded><![CDATA[<p>Its simple but i thought of leaving it here if i forget (which is usually the case) how to do it again.</p>
<p>If you want to insert the records from tableA into tableB, you do something like this:</p>
<p><span style="color: #999999;"><strong>INSERT INTO tableA SELECT * FROM tableB</strong></span></p>
<p>You have to make sure all fields are the same on both tables or you will get an error.</p>
<p>You can list the fields so you wont go wrong:</p>
<p><span style="color: #999999;"><strong>INSERT INTO tableA(name,address) SELECT name,address FROM tableB</strong></span></p>
<p>And you can even prevent the key field from being the same:</p>
<p><span style="color: #999999;"><strong><span>INSERT INTO tableA SELECT * FROM tableB ON DUPLICATE KEY UPDATE tableB.state =&#8221;changed&#8221;</span></strong></span></p>
<p>Like i mentioned, simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=10</wfw:commentRss>
		</item>
		<item>
		<title>Number formatting/padding before and after the decimal</title>
		<link>http://manifesto42.com/42/?p=8</link>
		<comments>http://manifesto42.com/42/?p=8#comments</comments>
		<pubDate>Mon, 06 Oct 2008 17:04:25 +0000</pubDate>
		<dc:creator>42</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[format]]></category>

		<category><![CDATA[number]]></category>

		<guid isPermaLink="false">http://manifesto42.com/42/?p=8</guid>
		<description><![CDATA[Before i came up with this solution, i had some trouble formatting and padding decimal numbers, specifically when i want to do it before and after the decimal mark. To top it all, i wanted the result signed (with a plus sign(+) or minus sign(-) depending on the value of the variable)
Example:
I want this &#8220;-4.3&#8243; [...]]]></description>
			<content:encoded><![CDATA[<p>Before i came up with this solution, i had some trouble formatting and padding decimal numbers, specifically when i want to do it before and after the decimal mark. To top it all, i wanted the result signed (with a plus sign(+) or minus sign(-) depending on the value of the variable)</p>
<p>Example:</p>
<p>I want this &#8220;-4.3&#8243; into this &#8220;-04.3&#8243;</p>
<p>Or this &#8220;6&#8243; into this &#8220;+06.0&#8243;</p>
<p>PHP has a lot of number formatting options, from number_format, to sprontf and printf. Well, just search the php docs and you will see what i mean.</p>
<p>The thing is that, as always, i wanted to do it in as less code as possible so, to do what i mentioned above, and after a bit of testing, i came up with this:</p>
<p><code>$value = -14.6;<br />
printf('%1$s%2$02d.%3$01s', ($value &lt;0 ) ? '-' : '+',floor(abs($value)),(abs($value)-floor(abs($value)))*10);</code></p>
<p>Although pretty simple, i think its still a bit contrived and dirty. But it works.</p>
<p>A bit of background:</p>
<p>the first part of the printf means:</p>
<p>%1 will contain the first parameter (the value after the first comma), with $s as the format (in sprintf, &#8217;s&#8217; means string, so it will just output the result of the first parameter, no formatting)</p>
<p>%2 has $02d as format so it will show a pad of two digits of an integer treated number showed as decimal</p>
<p>%3 has $01s as format - like i mentioned for %1, shows a string with a pad of one digit</p>
<p>Since i want to keep these posts simple, to serve as reference and jump to other reference sites, i will just leave some links:</p>
<p><a href="http://www.php.net/sprintf">PHP docs for sprintf</a> (it has all the formatting&#8230;formats):</p>
<p><a href="http://www.w3schools.com/php/php_ref_math.asp">W3C for PHP math:</a></p>
<p>Just ask if you need any more feedback or help.</p>
]]></content:encoded>
			<wfw:commentRss>http://manifesto42.com/42/?feed=rss2&amp;p=8</wfw:commentRss>
		</item>
	</channel>
</rss>
