<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Commentaires sur : Bash&#160;: convertir un string en entier</title>
	<atom:link href="http://pioupioum.fr/snippets/bash-convertir-string-to-integer.html/feed/" rel="self" type="application/rss+xml" />
	<link>http://pioupioum.fr/snippets/bash-convertir-string-to-integer.html</link>
	<description>Bloc-note d&#039;un développeur web</description>
	<lastBuildDate>Fri, 03 Sep 2010 07:29:57 +0200</lastBuildDate>
	
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>Par : patpro</title>
		<link>http://pioupioum.fr/snippets/bash-convertir-string-to-integer.html#comment-1165</link>
		<dc:creator>patpro</dc:creator>
		<pubDate>Sun, 07 Mar 2010 08:50:32 +0000</pubDate>
		<guid isPermaLink="false">http://pioupioum.fr/?p=340#comment-1165</guid>
		<description>&lt;div class=&quot;comment-content&quot;&gt;&lt;p&gt;ça semble bien fonctionner :)
Par contre j&#039;ai pas le temps de voir si on peut faire plus simple. Plus tard sans doute !&lt;/p&gt;
&lt;/div&gt;</description>
		<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="comment-content"><p>ça semble bien fonctionner <img src='http://pioupioum.fr/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
Par contre j&#8217;ai pas le temps de voir si on peut faire plus simple. Plus tard sans doute&#160;!</p>
</div><!-- google_ad_section_end -->]]></content:encoded>
	</item>
	<item>
		<title>Par : piouPiouM</title>
		<link>http://pioupioum.fr/snippets/bash-convertir-string-to-integer.html#comment-1162</link>
		<dc:creator>piouPiouM</dc:creator>
		<pubDate>Sun, 07 Mar 2010 03:24:57 +0000</pubDate>
		<guid isPermaLink="false">http://pioupioum.fr/?p=340#comment-1162</guid>
		<description>&lt;div class=&quot;comment-content&quot;&gt;&lt;p&gt;Bien vu :-/&lt;br /&gt;
J&#039;ai pondu le code qui suit du coup, qu&#039;en penses-tu ?&lt;/p&gt;

&lt;pre lang=&quot;bash&quot; line=&quot;1&quot;&gt;
#!/usr/bin/env bash
function string_to_int ()
{
    LANG=C
    d=${1##*.} # partie décimale
    if [[ ${#1} -eq ${#d} ]]; then
        d=0
    fi
    e=${1%.*}  # partie entière
    e=${e//,/}
    printf %.0f &quot;$e.$d&quot; 2&gt;/dev/null
}

not_int=(&quot;12.652&quot; &quot;-12.652&quot; 12.652 14 &quot;12,652&quot; foo &quot;1,254.8&quot; 1,254.8 &quot;125,160,254.8&quot; &quot;125,160,254&quot;)

for value in ${not_int[@]}; do
    echo &quot;Convert $value to int: $(string_to_int $value)&quot;
done&lt;/pre&gt;

&lt;p&gt;Il impose du coup l&#039;usage du point &lt;code&gt;.&lt;/code&gt; en tant que séparateur des décimales et la virgule &lt;code&gt;,&lt;/code&gt; comme séparateur dans la partie entière (non obligatoire).&lt;/p&gt;
&lt;/div&gt;</description>
		<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="comment-content"><p>Bien vu :-/<br />
J&#8217;ai pondu le code qui suit du coup, qu&#8217;en penses-tu&#160;?</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env bash</span>
<span style="color: #000000; font-weight: bold;">function</span> string_to_int <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">LANG</span>=C
    <span style="color: #007800;">d</span>=<span style="color: #800000;">${1##*.}</span> <span style="color: #666666; font-style: italic;"># partie décimale</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #800000;">${#1}</span> <span style="color: #660033;">-eq</span> <span style="color: #800000;">${#d}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">d</span>=<span style="color: #000000;">0</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #007800;">e</span>=<span style="color: #800000;">${1%.*}</span>  <span style="color: #666666; font-style: italic;"># partie entière</span>
    <span style="color: #007800;">e</span>=<span style="color: #800000;">${e//,/}</span>
    <span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #000000; font-weight: bold;">%</span>.0f <span style="color: #ff0000;">&quot;<span style="color: #007800;">$e</span>.<span style="color: #007800;">$d</span>&quot;</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #007800;">not_int</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;12.652&quot;</span> <span style="color: #ff0000;">&quot;-12.652&quot;</span> <span style="color: #000000;">12.652</span> <span style="color: #000000;">14</span> <span style="color: #ff0000;">&quot;12,652&quot;</span> foo <span style="color: #ff0000;">&quot;1,254.8&quot;</span> <span style="color: #000000;">1</span>,<span style="color: #000000;">254.8</span> <span style="color: #ff0000;">&quot;125,160,254.8&quot;</span> <span style="color: #ff0000;">&quot;125,160,254&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> value <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${not_int[@]}</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Convert <span style="color: #007800;">$value</span> to int: <span style="color: #007800;">$(string_to_int $value)</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>




<p>Il impose du coup l&#8217;usage du point <code>.</code> en tant que séparateur des décimales et la virgule <code>,</code> comme séparateur dans la partie entière (non obligatoire).</p>
</div><!-- google_ad_section_end -->]]></content:encoded>
	</item>
	<item>
		<title>Par : patpro</title>
		<link>http://pioupioum.fr/snippets/bash-convertir-string-to-integer.html#comment-1159</link>
		<dc:creator>patpro</dc:creator>
		<pubDate>Sat, 06 Mar 2010 21:39:02 +0000</pubDate>
		<guid isPermaLink="false">http://pioupioum.fr/?p=340#comment-1159</guid>
		<description>&lt;div class=&quot;comment-content&quot;&gt;&lt;p&gt;hmmm. Ça marche parce que tu es en LANG=fr_FR, mais teste ton script comme ça :&lt;/p&gt;

&lt;pre lang=&quot;bash&quot;&gt;LANG=C ./string_to_int.sh&lt;/pre&gt;

&lt;p&gt;Tu peux rendre ton script un peu plus générique en remplaçant ta fonction par :&lt;/p&gt;

&lt;pre lang=&quot;bash&quot;&gt;function string_to_int ()
{
    LANG=C
    printf %.0f ${1/,/.}
}&lt;/pre&gt;

&lt;p&gt;Néanmoins, ça ne te sauvera pas en cas de nombres comme 1,254.8 (notation US pour 1254,8), car de toute manière, locale ou pas, ce cas n&#039;est pas géré par printf.&lt;/p&gt;
&lt;/div&gt;</description>
		<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="comment-content"><p>hmmm. Ça marche parce que tu es en LANG=fr_FR, mais teste ton script comme ça&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">LANG</span>=C .<span style="color: #000000; font-weight: bold;">/</span>string_to_int.sh</pre></div></div>




<p>Tu peux rendre ton script un peu plus générique en remplaçant ta fonction par&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> string_to_int <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">LANG</span>=C
    <span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #000000; font-weight: bold;">%</span>.0f <span style="color: #800000;">${1/,/.}</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>




<p>Néanmoins, ça ne te sauvera pas en cas de nombres comme 1,254.8 (notation US pour 1254,8), car de toute manière, locale ou pas, ce cas n&#8217;est pas géré par printf.</p>
</div><!-- google_ad_section_end -->]]></content:encoded>
	</item>
</channel>
</rss>
