<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>how to round off the return value</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/19626/how-to-round-off-the-return-value</link><description> 
hello anyone here wants to help me...i want the return value to be
rounded off for example: 
TotalComponent=235 
PartialComponent=90 
per=38.3% 

 
but with this function i always get 0 % anyone here want to sugest
what should be done 

 
unsigned int</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/123444?ContentTypeID=1</link><pubDate>Mon, 16 Oct 2006 02:52:09 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b8d8a131-61f4-4781-87f8-1efe82f1a3b0</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;pre&gt;
 per = CalculatePercentage( 235, 90 );
 printf( &amp;quot;Percent %d&amp;quot;, per );
&lt;/pre&gt;

&lt;p&gt;
&lt;i&gt;the return value is 38% the .3 is disregarded&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
As Mike says, the reson for this should be obvious - it&amp;#39;s textbook
stuff.&lt;/p&gt;

&lt;pre&gt;
main() {
 float per;
 per = CalculatePercentage( 235, 90 );
 printf( &amp;quot;Percent %f&amp;quot;, per );
}
&lt;/pre&gt;

&lt;p&gt;
&lt;i&gt;the return value is 38% the .3 is disregarded&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
How have you checked the &lt;b&gt;return value&lt;/b&gt;?&lt;/p&gt;

&lt;p&gt;
Obviously, if the &lt;b&gt;return value&lt;/b&gt; has been truncated before
calling printf, then just changing the format specifier in the printf
cannot magically re-generate the discarded information!&lt;/p&gt;

&lt;p&gt;
Or maybe you just changed your source code, and forgot to rebuild
it...?&lt;/p&gt;

&lt;p&gt;
PS Note how the addition of a little whitespace makes your code so
much easier to read!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/112377?ContentTypeID=1</link><pubDate>Mon, 16 Oct 2006 02:23:11 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:25d661be-4a91-411a-a5a1-d586019937da</guid><dc:creator>Mik Kleshov</dc:creator><description>&lt;p&gt;&lt;p&gt;
Isn&amp;#39;t it obvious?&lt;br /&gt;
You &lt;b&gt;really really&lt;/b&gt; need to read a book on C. Getting answers to
questions like these in forums only postpones the inevitable -
reading the books.&lt;/p&gt;

&lt;p&gt;
- mike&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/98530?ContentTypeID=1</link><pubDate>Mon, 16 Oct 2006 01:09:32 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f68cfd94-d659-4fba-a1cb-334850b35d96</guid><dc:creator>meredith grey</dc:creator><description>&lt;p&gt;&lt;p&gt;
even if i change per to float&lt;/p&gt;

&lt;pre&gt;
main(){
float per;
 per=CalculatePercentage(235,90);
 printf(&amp;quot;Percent %f&amp;quot;,per);
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
the return value is 38% the .3 is disregarded&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/74446?ContentTypeID=1</link><pubDate>Mon, 16 Oct 2006 00:48:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:076a615f-b3d3-4688-af9a-1b99c7999295</guid><dc:creator>meredith grey</dc:creator><description>&lt;p&gt;&lt;p&gt;
hello mike tried your code how will i know if it return 38.3% i
transfered the code in c&lt;/p&gt;

&lt;pre&gt;
 per=CalculatePercentage(235,90);
 printf(&amp;quot;Percent %d&amp;quot;,per);
&lt;/pre&gt;

&lt;p&gt;
the return value is 38% the .3 is disregarded&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/74444?ContentTypeID=1</link><pubDate>Mon, 16 Oct 2006 00:30:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3f270cef-e15e-4d2a-b419-1eceb4679b34</guid><dc:creator>meredith grey</dc:creator><description>&lt;p&gt;&lt;p&gt;
hello mike im sori thats suppose to be&lt;/p&gt;

&lt;pre&gt;
return per;
&lt;/pre&gt;

&lt;p&gt;
okie i&amp;#39;ll try your code thanks for the reply&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/47015?ContentTypeID=1</link><pubDate>Mon, 16 Oct 2006 00:00:25 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d40fa1ed-6e7c-4de2-ba98-35c96e228d18</guid><dc:creator>Mik Kleshov</dc:creator><description>&lt;p&gt;&lt;p&gt;
Try this:&lt;/p&gt;

&lt;pre&gt;
unsigned int CalculatePercentage(int TotalComponent,int PartialComponent){
return 100.0f*PartialComponent/TotalComponent;
}
&lt;/pre&gt;

&lt;p&gt;
You might want to read a C book, specifically a section on types
and conversions between them. Otherwise prepare for more surprises
like this one. Regards, - mike&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/87974?ContentTypeID=1</link><pubDate>Sun, 15 Oct 2006 23:07:26 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c78b614b-ee2b-4230-ae79-208904b23ae4</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
Pardon me. I said &lt;i&gt;&amp;quot;... one of which divides into the other to
yield a signed int, which is further &lt;b&gt;divided by an int&lt;/b&gt;
...&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
That should be &lt;b&gt;multiplied by an int&lt;/b&gt;. Multiplying the
integer result of division does not suddenly cause a fractional
result of the division to appear. Like I said, think about the &amp;#39;type
of&amp;#39; and &amp;#39;results of&amp;#39; your subexpressions.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to round off the return value</title><link>https://community.arm.com/thread/47009?ContentTypeID=1</link><pubDate>Sun, 15 Oct 2006 22:52:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e63eebb8-4cd1-4119-9b59-627f80b4ce9e</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;per=38.3&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
No, per is a signed integer as are the function parameters, one of
which divides into the other to yield a signed int, which is further
divided by an int and the integer result of all that is assigned to
the int named &amp;#39;per&amp;#39;. Casting an int to a float yields a
non-fractional float. Returning that temporary float value as an
unsigned int merely truncates the fractional part of the float that
in this case never existed.&lt;/p&gt;

&lt;p&gt;
Think about how to make some subexpressions floating point or
scaled fixed point integers and how to adjust your return type or
your interpretation of the return type to suit your needs.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>