Re: [PHP] sprintf thousand separator.

2009-02-11 Thread chris smith
2009/2/11 João Cândido de Souza Neto j...@consultorweb.cnt.br:
 Hello everybody.

 I´d just like to know if there´s any way of defining a thousand separator to
 a currency value using sprintf.

Use number_format instead of sprintf, it's designed to do what you want.

-- 
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf thousand separator.

2009-02-11 Thread Jo�o C�ndido de Souza Neto
Chris,

Thanks for your answer, but my real problem is to get thousand separator in 
jpgraph class which uses sprintf to display almost everithing;


chris smith dmag...@gmail.com escreveu na mensagem 
news:3c1395330902110457i18cec69cy9818e08cdd3ff...@mail.gmail.com...
2009/2/11 João Cândido de Souza Neto j...@consultorweb.cnt.br:
 Hello everybody.

 I´d just like to know if there´s any way of defining a thousand separator 
 to
 a currency value using sprintf.

Use number_format instead of sprintf, it's designed to do what you want.

-- 
Postgresql  php tutorials
http://www.designmagick.com/ 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf thousand separator.

2009-02-11 Thread Per Jessen
chris smith wrote:

 2009/2/11 João Cândido de Souza Neto j...@consultorweb.cnt.br:
 Hello everybody.

 I´d just like to know if there´s any way of defining a thousand
 separator to a currency value using sprintf.
 
 Use number_format instead of sprintf, it's designed to do what you
 want.
 

I wonder why number_format() isn't locale sensitive?  


/Per

-- 
Per Jessen, Zürich (2.4°C)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf thousand separator.

2009-02-11 Thread Richard Heyes
 Thanks for your answer, but my real problem is to get thousand separator in
 jpgraph class which uses sprintf to display almost everithing;

Can you format it first, and then pass it to JPGraph ?

-- 
Richard Heyes

HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari:
http://www.rgraph.org (Updated January 31st)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf thousand separator.

2009-02-11 Thread Jo�o C�ndido de Souza Neto
No, I can´t, because if I do it how can jpgrhph render without numeric data? 
hehehe

Richard Heyes rich...@php.net escreveu na mensagem 
news:af8726440902110523x63ce5485p6534d10063eb4...@mail.gmail.com...
 Thanks for your answer, but my real problem is to get thousand separator 
 in
 jpgraph class which uses sprintf to display almost everithing;

 Can you format it first, and then pass it to JPGraph ?

 -- 
 Richard Heyes

 HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari:
 http://www.rgraph.org (Updated January 31st) 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf thousand separator.

2009-02-11 Thread Bruno Fajardo
Can you extend the JPGraph class, intercepting the desired method,
formatting the output the way you need?

2009/2/11 João Cândido de Souza Neto j...@consultorweb.cnt.br

 No, I can´t, because if I do it how can jpgrhph render without numeric data?
 hehehe

 Richard Heyes rich...@php.net escreveu na mensagem
 news:af8726440902110523x63ce5485p6534d10063eb4...@mail.gmail.com...
  Thanks for your answer, but my real problem is to get thousand separator
  in
  jpgraph class which uses sprintf to display almost everithing;
 
  Can you format it first, and then pass it to JPGraph ?
 
  --
  Richard Heyes
 
  HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari:
  http://www.rgraph.org (Updated January 31st)



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf thousand separator.

2009-02-11 Thread Jo�o C�ndido de Souza Neto
Ok, thanks.

I´ll try to do it.

As soon as I get it I´ll let you know.

Thanks guys.

Bruno Fajardo bsfaja...@gmail.com escreveu na mensagem 
news:eeb6980b0902110534p22a8c75eqd98cceaf3a150...@mail.gmail.com...
Can you extend the JPGraph class, intercepting the desired method,
formatting the output the way you need?

2009/2/11 João Cândido de Souza Neto j...@consultorweb.cnt.br

 No, I can´t, because if I do it how can jpgrhph render without numeric 
 data?
 hehehe

 Richard Heyes rich...@php.net escreveu na mensagem
 news:af8726440902110523x63ce5485p6534d10063eb4...@mail.gmail.com...
  Thanks for your answer, but my real problem is to get thousand 
  separator
  in
  jpgraph class which uses sprintf to display almost everithing;
 
  Can you format it first, and then pass it to JPGraph ?
 
  --
  Richard Heyes
 
  HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari:
  http://www.rgraph.org (Updated January 31st)



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf problem... (with simplified code )

2008-03-23 Thread Richard
Sorry, this morning I have gone through everything again, and the 
simplified example was wrong, but corrected it gives the right answer, 
however although I'm sure I've done the same with the main script I can 
not get the correct answer.


I need to round each article seperatly so that the Vat inc prince 
remains the same.


So a 10.03 without vat always makes 12.00 with vat.

My test files works as it should :

?php
$total = 0;
$ht = 10.03;
$vat = 19.6;
$total += sprintf(%0.2f,$ht*($vat/100+1));
print(Total = .sprintf(%0.2f,$total));
$total += sprintf(%0.2f,$ht*($vat/100+1));
print( - Total 2 = .sprintf(%0.2f,$total));
?

Result : Total : 12 - Total 2 : 24

However the file I'm working on does not :(

The script I'm working on makes use of the fpdf php project which 
automaticaly creates PDF files.


Here is the extract of code :

while ( list($code_tva, $articleHT) = each( $px ) )
{
$tva = $tab_tva[$code_tva]; // is 19.6 %
$this-SetXY(15, $y); //sets the coordinates of cell to be created in 
PDF file for each item )
$this-Cell( 21.5,4, sprintf(%0.2f, $articleHT),'', '','C' ); // 
inserts the item
//= $totalHT is TOTAL without VAT and 
$articleHT is a table which contains the price of each item without tax

$totalHT += $articleHT; //adds all items without vat
//= $totalTTC is TOTAL with VAT 
$totalTTC += sprintf(%0.2f, ($articleHT * ( 1 + $tva/100 ))); // adds 
the rounded (item * 1.196) (including vat)

//= $totalTVA is TOTAL of VAT 
$tmp_tva = sprintf(%0.2f,$articleHT * $tva/100); //amount of VAT of 
current item

$a_tva[ $code_tva ] = $tmp_tva;
$totalTVA += $tmp_tva; //adds current item's vat to total vat
$this-SetXY(10, $y);
$this-Cell( 5,4, $code_tva, 0,0,C); //prints vat option
$this-SetXY(58, $y);
$this-Cell( 21.5,4, sprintf(%0.2f,$tmp_tva),'', '' ,'C'); //prints 
the current item's vat amount

$this-SetXY(79.5, $y);
$this-Cell( 21.5,4, sprintf(%0.2f,$tva) ,'', '', 'C'); //prints the 
vat percentage for item

$y+=4;
}
$this-SetXY(114,266.4);
$this-Cell(15,4, sprintf(%0.2f, $totalHT), '', '', 'R' ); //prints 
the total amount excluding vat

$this-SetXY(114,271.4);
$this-Cell(15,4, sprintf(%0.2f, $totalTVA), '', '', 'R' ); //prints 
total amount including vat


This is how I see it :
If I have two different items ($articleHT) at 10.03 this line ($tva = 
19.6 and $articleHT = 10.03) :

$totalTTC += sprintf(%0.2f, ($articleHT * ( 1 + $tva/100 )));
Should add 12.00 to $totalTTC However it seems to add 11.99588 to 
$totalTTC instead ...


In my test files it does exactly this, any idea why it would not do this 
in the real file ?


Thanks again :)


lists-php a écrit :

your issue is with rounding, and whether vat is applied (rounded)
per-item or on the invoice total.

your sprintf output is simply truncating and rounding on display. you
aren't storing the rounded (up) value.

increase the decimal positions to 4 or 6 and you'll see things better.

  Total = 11.995880 - Total 2 = 23.991760

the vat on 10.03 is 1.96588, which when you only display 2 positions
on display rounds up to 1.97. 


if you stored the rounded value ,on a per-item basis, you'd get what
you're after.

now, the question is whether vat is applied per item or on the
invoice total.

the difference becomes more obvious with say 3 items, at 10.03. on a
(rounded) per-item basis the vat total would be 5.91. on an invoice
total it would be 5.90 (rounded up from 5.89784).


  - Rick


 Original Message 
  

Date: Saturday, March 22, 2008 10:35:12 PM +0100
From: Richard [EMAIL PROTECTED]
To: PHP lists php-general@lists.php.net
Cc: 
Subject: Re: [PHP] sprintf problem... (with simplified code )


Here is a simplifie version of my code :

?php
$total = 0;
$ht = 10.03;
$vat = 19.6;
$total += $ht*($vat/100+1);
print(Total = .sprintf(%0.2f, $total));
$total += $ht*($vat/100+1);
print( - Total 2 = .sprintf(%0.2f, $total));
?

I get 12.00 and 23.99 ... what should I do to get 12.00 and 24.00 ?


Richard a écrit :


Hello, I'm trying to write a invoice script but I have encountered
a  slight problem.
Each product needs to be listed without tax, and at the end of the 
file I need to show the VAT. In France VAT is 19.6%

So €10.03 without vat = €12.00 with vat.
So I do this :

$totalwithoutvat = 0;
$totalwithoutvat = 0;
$totalvat =0;
while ( $itemwithoutvat) = each( $px ) )
{
$totalwithoutvat += $itemwithoutvat;
$totalwithoutvat += sprintf(%0.2f, $itemwithoutvat * ( 1 +
19.6/100 )); $totalvat += sprintf(%0.2f,$itemwithoutvat *
19.6/100); }
print (Total Vat =.sprintf(%0.2f,$totalvat). - Total without
vat  = .sprintf(%0.2f,$totalwithoutvat). - Total with vat = 
.sprintf(%0.2f,$totalwithvat));


But I'm not sure I am using sprintf correctly, maybe I should use
a  different function because :
When I have one item at 10.03, the result is : Total Vat = 1.97 - 
Total without vat = 10.03 Total with vat = 12.00

but if I have two items at 10.03 the result is : Total vat

Re: [PHP] sprintf problem...

2008-03-23 Thread Daniel Brown
On Sat, Mar 22, 2008 at 3:37 PM, Richard [EMAIL PROTECTED] wrote:
[snip=code!]
  When I have one item at 10.03, the result is : Total Vat = 1.97 - Total
  without vat = 10.03 Total with vat = 12.00
  but if I have two items at 10.03 the result is : Total vat = 3.93 -
  Total without vat = 20.06 Total with vat = 23.99
  but I need it to be : Total vat = 3.94 - Total without vat = 20.06 Total
  with vat = 24.00

Check your math, Richard.  You're using sprintf() with the float
correctly, you're just missing how it's rounding.

(10.03 * 19.6) / 100 = 1.96588 (rounded to 1.97)
((10.03 * 19.6) / 100) * 2 = 3.93176 (rounded to 3.93)

If you want it to come out to an even number with the VAT added,
simply round the amount first, then multiply it.

-- 
/Daniel P. Brown
Forensic Services, Senior Unix Engineer
1+ (570-) 362-0283

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sprintf problem...

2008-03-22 Thread Richard
Hello, I'm trying to write a invoice script but I have encountered a 
slight problem.
Each product needs to be listed without tax, and at the end of the file 
I need to show the VAT. In France VAT is 19.6%

So €10.03 without vat = €12.00 with vat.
So I do this :

$totalwithoutvat = 0;
$totalwithoutvat = 0;
$totalvat =0;
while ( $itemwithoutvat) = each( $px ) )
{
$totalwithoutvat += $itemwithoutvat;
$totalwithoutvat += sprintf(%0.2f, $itemwithoutvat * ( 1 + 19.6/100 ));
$totalvat += sprintf(%0.2f,$itemwithoutvat * 19.6/100);
}
print (Total Vat =.sprintf(%0.2f,$totalvat). - Total without vat = 
.sprintf(%0.2f,$totalwithoutvat). - Total with vat = 
.sprintf(%0.2f,$totalwithvat));


But I'm not sure I am using sprintf correctly, maybe I should use a 
different function because :
When I have one item at 10.03, the result is : Total Vat = 1.97 - Total 
without vat = 10.03 Total with vat = 12.00
but if I have two items at 10.03 the result is : Total vat = 3.93 - 
Total without vat = 20.06 Total with vat = 23.99
but I need it to be : Total vat = 3.94 - Total without vat = 20.06 Total 
with vat = 24.00
So from what I can see, sprintf only seems to work for printing and the 
actual result is kept in memory, is this correct ? if so what function 
should I use instead?


Thanks in advance :)

Richard









--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf problem... (with simplified code )

2008-03-22 Thread Richard

Here is a simplifie version of my code :

?php
$total = 0;
$ht = 10.03;
$vat = 19.6;
$total += $ht*($vat/100+1);
print(Total = .sprintf(%0.2f, $total));
$total += $ht*($vat/100+1);
print( - Total 2 = .sprintf(%0.2f, $total));
?

I get 12.00 and 23.99 ... what should I do to get 12.00 and 24.00 ?


Richard a écrit :
Hello, I'm trying to write a invoice script but I have encountered a 
slight problem.
Each product needs to be listed without tax, and at the end of the 
file I need to show the VAT. In France VAT is 19.6%

So €10.03 without vat = €12.00 with vat.
So I do this :

$totalwithoutvat = 0;
$totalwithoutvat = 0;
$totalvat =0;
while ( $itemwithoutvat) = each( $px ) )
{
$totalwithoutvat += $itemwithoutvat;
$totalwithoutvat += sprintf(%0.2f, $itemwithoutvat * ( 1 + 19.6/100 ));
$totalvat += sprintf(%0.2f,$itemwithoutvat * 19.6/100);
}
print (Total Vat =.sprintf(%0.2f,$totalvat). - Total without vat 
= .sprintf(%0.2f,$totalwithoutvat). - Total with vat = 
.sprintf(%0.2f,$totalwithvat));


But I'm not sure I am using sprintf correctly, maybe I should use a 
different function because :
When I have one item at 10.03, the result is : Total Vat = 1.97 - 
Total without vat = 10.03 Total with vat = 12.00
but if I have two items at 10.03 the result is : Total vat = 3.93 - 
Total without vat = 20.06 Total with vat = 23.99
but I need it to be : Total vat = 3.94 - Total without vat = 20.06 
Total with vat = 24.00
So from what I can see, sprintf only seems to work for printing and 
the actual result is kept in memory, is this correct ? if so what 
function should I use instead?


Thanks in advance :)

Richard



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-03 Thread Andrew Ballard
On Dec 1, 2007 11:30 AM, tedd [EMAIL PROTECTED] wrote:
 At 7:10 AM -0500 12/1/07, Christoph Boget wrote:
 Why does
 
 sprintf( '%.03f', 0.1525 )
 
 return 0.152 while
 
 sprintf( '%.03f', 0.1575 )
 
 return 0.158?
 
 The 4th significant digit in both cases is '5' but in the first case, it's
 rounded down but in the second case it is rounded up.  Is sprintf() basing
 it's decision on the value of the 3rd significant digit?  If so, why?
 Shouldn't rounding decisions be based on subsequent digits and not preceding
 ones?
 
 I am using PHP 4.3.11
 
 thnx,
 Christoph


 Actually, it's more accurate to use the preceding digit to determine
 rounding. What you describe is if even then round down else round
 up. That's more accurate than always rounding up or always rounding
 down.

 Please follow:

 When the digit is zero, no rounding takes place.
 When the digit is 1 - 4, round down.
 When the digit is 6 - 9, round up.

 Fine up to there -- but, if you always round up or down for 5, then
 you are introducing bias.

 So, if you use the even/odd value of the preceding digit to determine
 which way to round for 5, then it's more accurate.

 However, no rounding is accurate -- it's just that some methods are
 less error prone.

 Here's a demo:

 http://www.webbytedd.com/bbb/rounding/

 My rounding method doesn't always win against php's round(), but it
 wins more times than not. Plus, it takes a lot of rounding to
 introduce the bias I found and thus for most applications using the
 round() function (always round down) will suffice.

 Cheers,

 tedd

 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



Oh, man I'm having flashbacks to a flame war that broke out on a
SQL Server board I read related to bankers rounding versus
traditional rounding and which was more correct or accurate.
:-)

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-03 Thread tedd

At 2:04 PM -0500 12/3/07, Andrew Ballard wrote:


Oh, man I'm having flashbacks to a flame war that broke out on a
SQL Server board I read related to bankers rounding versus
traditional rounding and which was more correct or accurate.
:-)

Andrew


There really shouldn't be a debate on which is more accurate, because 
that's easily enough to determine.


One can argue if it makes a difference, or doing it more correctly 
takes too much time, or other such considerations, but how accurate 
is usually determined as the least amount of error produced by a 
given calculation method.


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-03 Thread Andrew Ballard
On Dec 3, 2007 3:29 PM, tedd [EMAIL PROTECTED] wrote:
 At 2:04 PM -0500 12/3/07, Andrew Ballard wrote:
 
 Oh, man I'm having flashbacks to a flame war that broke out on a
 SQL Server board I read related to bankers rounding versus
 traditional rounding and which was more correct or accurate.
 :-)
 
 Andrew

 There really shouldn't be a debate on which is more accurate, because
 that's easily enough to determine.

 One can argue if it makes a difference, or doing it more correctly
 takes too much time, or other such considerations, but how accurate
 is usually determined as the least amount of error produced by a
 given calculation method.


 Cheers,

 tedd


Which is more accurate? Neither - they're both wrong. :-P

Uh oh. I may have ignited what I wanted to avoid. Seriously, though,
you're probably right. I can't find the thread now, but I seem to
recall that the bulk of the discussion centered around someone on the
list arguing that the difference between each method was so small that
the benefit was negligible, especially compared to the overhead of
using a custom function in SQL Server to do what the client asked (to
use banker's rounding) versus using the native ROUND function. After
all, clients can be so irritating when they expect you to build a
system the way they want it. :-)

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-02 Thread Christoph

The 4th significant digit in both cases is '5' but in the first case, it's
rounded down but in the second case it is rounded up.  Is sprintf() basing
it's decision on the value of the 3rd significant digit?  If so, why?
Shouldn't rounding decisions be based on subsequent digits and not 
preceding

ones?
Actually, it's more accurate to use the preceding digit to determine 
rounding. What you describe is if even then round down else round up. 
That's more accurate than always rounding up or always rounding down.

Please follow:
When the digit is zero, no rounding takes place.
When the digit is 1 - 4, round down.
When the digit is 6 - 9, round up.


Ok, I understand what's gong on but still not why.

Fine up to there -- but, if you always round up or down for 5, then you 
are introducing bias.


Fair point except...

So, if you use the even/odd value of the preceding digit to determine 
which way to round for 5, then it's more accurate.
My rounding method doesn't always win against php's round(), but it wins 
more times than not. Plus, it takes a lot of rounding to introduce the 
bias I found and thus for most applications using the round() function 
(always round down) will suffice.


...consider the following example:

script language=php
echo 'h3sprintf() test:brbr';
echo 'sprintf( %.03f, 0.20375000 ) = ' . sprintf( '%.03f', 0.20375000 ) . 
'br';
echo 'sprintf( %.03f, 0.17318750 ) = ' . sprintf( '%.03f', 0.17318750 ) . 
'br';
echo 'sprintf( %.03f, 0.15485000 ) = ' . sprintf( '%.03f', 0.15485000 ) . 
'br';
echo 'sprintf( %.03f, 0.1525 ) = ' . sprintf( '%.03f', 0.1525 ) . 
'br';
echo 'sprintf( %.03f, 0.12962500 ) = ' . sprintf( '%.03f', 0.12962500 ) . 
'br';
echo 'sprintf( %.03f, 0.1159 ) = ' . sprintf( '%.03f', 0.1159 ) . 
'br';
echo 'sprintf( %.03f, 0.1575 ) = ' . sprintf( '%.03f', 0.1575 ) . 
'br';
echo 'sprintf( %.03f, 0.13387500 ) = ' . sprintf( '%.03f', 0.13387500 ) . 
'br';
echo 'sprintf( %.03f, 0.1197 ) = ' . sprintf( '%.03f', 0.1197 ) . 
'br';
echo 'sprintf( %.03f, 0.1525 ) = ' . sprintf( '%.03f', 0.1525 ) . 
'br';
echo 'sprintf( %.03f, 0.12962500 ) = ' . sprintf( '%.03f', 0.12962500 ) . 
'br';
echo 'sprintf( %.03f, 0.1159 ) = ' . sprintf( '%.03f', 0.1159 ) . 
'br';
echo 'sprintf( %.03f, 0.07875000 ) = ' . sprintf( '%.03f', 0.07875000 ) . 
'br';
echo 'sprintf( %.03f, 0.06693750 ) = ' . sprintf( '%.03f', 0.06693750 ) . 
'br';
echo 'sprintf( %.03f, 0.05985000 ) = ' . sprintf( '%.03f', 0.05985000 ) . 
'br';
echo 'sprintf( %.03f, 0.13125000 ) = ' . sprintf( '%.03f', 0.13125000 ) . 
'br';
echo 'sprintf( %.03f, 0.13375000 ) = ' . sprintf( '%.03f', 0.13375000 ) . 
'brbr';

echo 'h3round() test:brbr';
echo 'round( 0.20375000, 3 ) = ' . round( 0.20375000, 3 ) . 'br';
echo 'round( 0.17318750, 3 ) = ' . round( 0.17318750, 3 ) . 'br';
echo 'round( 0.15485000, 3 ) = ' . round( 0.15485000, 3 ) . 'br';
echo 'round( 0.1525, 3 ) = ' . round( 0.1525, 3 ) . 'br';
echo 'round( 0.12962500, 3 ) = ' . round( 0.12962500, 3 ) . 'br';
echo 'round( 0.1159, 3 ) = ' . round( 0.1159, 3 ) . 'br';
echo 'round( 0.1575, 3 ) = ' . round( 0.1575, 3 ) . 'br';
echo 'round( 0.13387500, 3 ) = ' . round( 0.13387500, 3 ) . 'br';
echo 'round( 0.1197, 3 ) = ' . round( 0.1197, 3 ) . 'br';
echo 'round( 0.1525, 3 ) = ' . round( 0.1525, 3 ) . 'br';
echo 'round( 0.12962500, 3 ) = ' . round( 0.12962500, 3 ) . 'br';
echo 'round( 0.1159, 3 ) = ' . round( 0.1159, 3 ) . 'br';
echo 'round( 0.07875000, 3 ) = ' . round( 0.07875000, 3 ) . 'br';
echo 'round( 0.06693750, 3 ) = ' . round( 0.06693750, 3 ) . 'br';
echo 'round( 0.05985000, 3 ) = ' . round( 0.05985000, 3 ) . 'br';
echo 'round( 0.13125000, 3 ) = ' . round( 0.13125000, 3 ) . 'br';
echo 'round( 0.13375000, 3 ) = ' . round( 0.13375000, 3 ) . 'brbr';
/script

Why is the behavior of round() different to that of sprintf()?  Both are 
rounding numbers.  I'll grant you that sprintf() has more utility and does 
more things than round() but when you are dealing with numbers (and more 
specifically, when you've used a numeric type specifier in the format 
argument), sprintf() is doing the same type of thing to the value --  
rounding.  So why are they doing it differently?  Is that a bug?  I'm 
reticent to say that but it doesn't make sense to me that they would behave 
differently.  If the general rule is to round up for 5s when preceeding is 
odd and round down when even, that's not occuring here when using round().


thnx,
Christoph 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-02 Thread tedd

At 6:08 AM -0500 12/2/07, Christoph wrote:
 If the general rule is to round up for 5s when preceeding is odd 
and round down when even, that's not occuring here when using 
round().


No, you're not reading what I wrote. I said MY general rule is to 
round up when the preceding digit is even, whereas php's round 
function always rounds down.


As for sprint_f(), I haven't investigated that.

Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-02 Thread Martin Alterisio
2007/12/1, Christoph Boget [EMAIL PROTECTED]:

 Why does

 sprintf( '%.03f', 0.1525 )

 return 0.152 while

 sprintf( '%.03f', 0.1575 )

 return 0.158?



Welcome to the world of f floating point numbers. Discrete mathematics,
leave all hope, ye that enter.

It's the way floating point number behave (a computational error that's more
problematic that one may suspect). You'll have to live with it, or use an
extension for arbitrary sized numbers (or fixed point numbers).

Why we have to accept this kind of behavior?

Long story short: they are excellent for computations required in 3d
graphics, they are lousy for business applications...
...and we all know that computers are for playing 3d games, don't we?

XD

Sorry about the noise.


[PHP] sprintf() oddness

2007-12-01 Thread Christoph Boget
Why does

sprintf( '%.03f', 0.1525 )

return 0.152 while

sprintf( '%.03f', 0.1575 )

return 0.158?

The 4th significant digit in both cases is '5' but in the first case, it's
rounded down but in the second case it is rounded up.  Is sprintf() basing
it's decision on the value of the 3rd significant digit?  If so, why?
Shouldn't rounding decisions be based on subsequent digits and not preceding
ones?

I am using PHP 4.3.11

thnx,
Christoph


Re: [PHP] sprintf() oddness

2007-12-01 Thread Per Jessen
Christoph Boget wrote:

 Why does
 
 sprintf( '%.03f', 0.1525 )
 return 0.152 while
 sprintf( '%.03f', 0.1575 )
 return 0.158?
 
 I am using PHP 4.3.11

I see the same behaviour in 5.2.4


/Per Jessen, Zürich

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-01 Thread Jochem Maas
Per Jessen wrote:
 Christoph Boget wrote:
 
 Why does

 sprintf( '%.03f', 0.1525 )
 return 0.152 while
 sprintf( '%.03f', 0.1575 )
 return 0.158?

most likely it's an artifact of the fact that
the binary representations of those floating point
values are not exact e.g. 0.1575 is represented
in binary as something like 0.15751

something like that.


 I am using PHP 4.3.11
 
 I see the same behaviour in 5.2.4
 
 
 /Per Jessen, Zürich
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-01 Thread Per Jessen
Jochem Maas wrote:

 Per Jessen wrote:
 Christoph Boget wrote:
 
 Why does

 sprintf( '%.03f', 0.1525 )
 return 0.152 while
 sprintf( '%.03f', 0.1575 )
 return 0.158?
 
 most likely it's an artifact of the fact that
 the binary representations of those floating point
 values are not exact e.g. 0.1575 is represented
 in binary as something like 0.15751
 
 something like that.

I just tried it in plain C - same result.  


/Per Jessen, Zürich

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddness

2007-12-01 Thread tedd

At 7:10 AM -0500 12/1/07, Christoph Boget wrote:

Why does

sprintf( '%.03f', 0.1525 )

return 0.152 while

sprintf( '%.03f', 0.1575 )

return 0.158?

The 4th significant digit in both cases is '5' but in the first case, it's
rounded down but in the second case it is rounded up.  Is sprintf() basing
it's decision on the value of the 3rd significant digit?  If so, why?
Shouldn't rounding decisions be based on subsequent digits and not preceding
ones?

I am using PHP 4.3.11

thnx,
Christoph



Actually, it's more accurate to use the preceding digit to determine 
rounding. What you describe is if even then round down else round 
up. That's more accurate than always rounding up or always rounding 
down.


Please follow:

When the digit is zero, no rounding takes place.
When the digit is 1 - 4, round down.
When the digit is 6 - 9, round up.

Fine up to there -- but, if you always round up or down for 5, then 
you are introducing bias.


So, if you use the even/odd value of the preceding digit to determine 
which way to round for 5, then it's more accurate.


However, no rounding is accurate -- it's just that some methods are 
less error prone.


Here's a demo:

http://www.webbytedd.com/bbb/rounding/

My rounding method doesn't always win against php's round(), but it 
wins more times than not. Plus, it takes a lot of rounding to 
introduce the bias I found and thus for most applications using the 
round() function (always round down) will suffice.


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sprintf() oddity

2006-04-18 Thread Chris Boget
What's going on here:
 
$number = 50.1234567890;
echo sprintf( '%.05f', $number );

result: 50.12346
 
$number = 5.1234567890;
echo sprintf( '%.05f', $number );

result: 5.12305
 
$number = 5000.1234567890;
echo sprintf( '%.05f', $number );

result: 5000.0
 
Why isn't it formatting the decimals properly? And apparently losing
some?
 
Running PHP 4.3.11 on Windows Server 2003 box.
 
thnx,
Chris


Re: [PHP] sprintf() oddity

2006-04-18 Thread Satyam
You are going beyond the number of significant digits PHP can handle so it 
is truncating beyond that.   In the second case it look sstrange, the 12305 
ending, but it is probably a 'round' number if you put it in binary, but it 
turns funny when converted to decimal.   In the last example, the precision 
does not even reach the decimal part.



- Original Message - 
From: Chris Boget [EMAIL PROTECTED]

To: php-general@lists.php.net
Sent: Tuesday, April 18, 2006 5:19 PM
Subject: [PHP] sprintf() oddity


What's going on here:

$number = 50.1234567890;
echo sprintf( '%.05f', $number );

result: 50.12346

$number = 5.1234567890;
echo sprintf( '%.05f', $number );

result: 5.12305

$number = 5000.1234567890;
echo sprintf( '%.05f', $number );

result: 5000.0

Why isn't it formatting the decimals properly? And apparently losing
some?

Running PHP 4.3.11 on Windows Server 2003 box.

thnx,
Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf() oddity

2006-04-18 Thread Richard Lynch
On Tue, April 18, 2006 10:19 am, Chris Boget wrote:
 What's going on here:

 $number = 50.1234567890;
 echo sprintf( '%.05f', $number );

 result: 50.12346

 $number = 5.1234567890;
 echo sprintf( '%.05f', $number );

 result: 5.12305

 $number = 5000.1234567890;
 echo sprintf( '%.05f', $number );

 result: 5000.0

 Why isn't it formatting the decimals properly? And apparently losing
 some?

 Running PHP 4.3.11 on Windows Server 2003 box.

PHP stores float/decimal numbers internally in 4-byte (32-bit) values
using standard C float libraries.

Rounding things off is guaranteed to produce results like this.

Though the one that ends in .12305 is a bit odd...

Still, if you NEED any sort of verifiable accuracy or especially large
scale, you'll have to use a custom library that deals with decimal
numbers as strings and allows you to choose arbitrary precision.

You'll sacrifice speed, of course.  And RAM.  How much you sacrifice
depends on how much accuracy you need, to some degree, but it's also
just inherently slower and more resource-intensive to do all the
calculations as strings.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf and arrays.

2005-04-07 Thread Duncan Hill
On Wednesday 06 April 2005 18:05, Brent Baisley typed:
 You want to have sprintf look at the contents of the variable instead
 of the variable itself. Anytime you want to do something like this you
 use the eval() function to evaluate the contents of the variable.

Woot!

$begin_string = 'htmlentities(sprintf($tmp[0],';
foreach ($p_sub_values as $k = $v) {
 $mid_string[] = \$p_sub_values[$k];
}
$mid_string = join(',', $mid_string);
$end_string = '), ENT_QUOTES);';
// Eval will stick the three strings together and then execute them.  Neat 
trick, hint from
// [EMAIL PROTECTED]
eval (\$r = $begin_string$mid_string$end_string);

Works a treat.  Thanks Brent.

-- 
My mind not only wanders, it sometimes leaves completely.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sprintf and arrays.

2005-04-06 Thread Duncan Hill
I have a snippet of code that looks something like:
if (is_array($p_sub_values)) {
foreach ($p_sub_values as $i = $v) {
 $p_sub_values_str[$i] = '$v';
}
$s = join(',', $p_sub_values_str);
$r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
}

$tmp[0] in this case contains a string like 'Fred likes %1$s on his %2$s', 
taking advantage of positional substitution with sprintf.

The function call to this snippet can have an optional array passed.  My 
need/desire is to substitute each element of the array into the appropriate 
position with sprintf.  So far I've tried:
$r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
$r = htmlentities(sprintf($tmp[0], ${$s}, ENT_QUOTES);

and a few other bits and pieces, all to no avail (error is about not enough 
arguments).

Is there any way to accomplish this in PHP, or do I need to roll my own 
substitution code?  The array can obviously be anything from a single value 
to 'unlimited' (though in practice will probably be less than 5).

-- 
My mind not only wanders, it sometimes leaves completely.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf and arrays.

2005-04-06 Thread Brent Baisley
For one, you are missing a right parenthesis ) in all of your examples.
htmlentities(  sprintf( $tmp[0], $s, ENT_QUOTES )
Second, the string you are trying to format only has one variable 
argument: $s.
   Fred likes %1$s on his %2$s
You have it numbered for ordering, but you are still ordering one 
variable. I never used sprintf like that, so I'm not sure if that is a 
valid way of doing it. That may be what is giving you the too few 
arguments error. Typically you would do something like:
   Fred likes %1$s on his %2$t
If you are using sprintf in a valid manner, then the problem is 
probably in the $s argument of the sprintf function. You are only 
specifying one variable to replace when you are looking to do two 
replacements. The contents of $s is a single string that happens to 
contains commas, it does not get evaluated as multiple arguments just 
because it contains commas. Try wrapping $s in  eval().
  htmlentities( sprintf( $tmp[0], eval($s) ), ENT_QUOTES);

Personally, I rolled my own basic search and replace string function to 
support my own templating tag system. I still use sprinf for more 
fancy stuff, but for basic stuff, I rolled my own. A simplified 
version is below. It accepts an associative array and a string as 
parameters and returns the merged result.

$text_str = And example for {::Name::} in answer to {::Question::} on 
the {::ListName::}. Hope it helps {::Name::}!;
$data_Merge['Name'] = Duncan Hill;
$data_Merge['Question'] = sprintf and arrays;
$data_Merge['ListName'] = PHP General;
echo mergeTplData($data_Merge, $text_str);

function mergeTplData($data, $str) {
$searchTags = array_keys($data);
$searchTags = '{::'.implode('::},{::',$searchTags).'::}';
$searchTags = explode(',',$searchTags);
$str = str_replace($searchTags,$data,$str);
return $str;
}
On Apr 6, 2005, at 7:23 AM, Duncan Hill wrote:
I have a snippet of code that looks something like:
if (is_array($p_sub_values)) {
foreach ($p_sub_values as $i = $v) {
 $p_sub_values_str[$i] = '$v';
}
$s = join(',', $p_sub_values_str);
$r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
}
$tmp[0] in this case contains a string like 'Fred likes %1$s on his 
%2$s',
taking advantage of positional substitution with sprintf.

The function call to this snippet can have an optional array passed.  
My
need/desire is to substitute each element of the array into the 
appropriate
position with sprintf.  So far I've tried:
$r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
$r = htmlentities(sprintf($tmp[0], ${$s}, ENT_QUOTES);

and a few other bits and pieces, all to no avail (error is about not 
enough
arguments).

Is there any way to accomplish this in PHP, or do I need to roll my own
substitution code?  The array can obviously be anything from a single 
value
to 'unlimited' (though in practice will probably be less than 5).

--
My mind not only wanders, it sometimes leaves completely.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search  Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sprintf and arrays.

2005-04-06 Thread Duncan Hill
On Wednesday 06 April 2005 16:40, Brent Baisley wrote:
 For one, you are missing a right parenthesis ) in all of your examples.
 htmlentities(  sprintf( $tmp[0], $s, ENT_QUOTES )

Meh, syntax blip from the cut and paste.

 Second, the string you are trying to format only has one variable
 argument: $s.
 Fred likes %1$s on his %2$s

The source array in this case has two entries.  My whole aim is to pass an 
array of n entries, unroll the array into a string and somehow interpolate 
the string into the sprintf call so that life is groovy.  I realise $s looks 
like a single variable to PHP at that point sprintf() is called, and this is 
what I'm trying to work around - some way to get sprintf to realise that $s 
is actually two strings.  Ie, I need a double level of interpolation on the 
$s value so that sprintf($tmp[0], $s) turns into sprintf(tmp[0], $array[0], 
$array[1]).  I'm starting to get the feeling that this will be impossible.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf and arrays.

2005-04-06 Thread Brent Baisley
Making life groovy has been difficult since the 60's.
You want to have sprintf look at the contents of the variable instead 
of the variable itself. Anytime you want to do something like this you 
use the eval() function to evaluate the contents of the variable.

On Apr 6, 2005, at 12:46 PM, Duncan Hill wrote:
On Wednesday 06 April 2005 16:40, Brent Baisley wrote:
For one, you are missing a right parenthesis ) in all of your 
examples.
htmlentities(  sprintf( $tmp[0], $s, ENT_QUOTES )
Meh, syntax blip from the cut and paste.
Second, the string you are trying to format only has one variable
argument: $s.
Fred likes %1$s on his %2$s
The source array in this case has two entries.  My whole aim is to 
pass an
array of n entries, unroll the array into a string and somehow 
interpolate
the string into the sprintf call so that life is groovy.  I realise $s 
looks
like a single variable to PHP at that point sprintf() is called, and 
this is
what I'm trying to work around - some way to get sprintf to realise 
that $s
is actually two strings.  Ie, I need a double level of interpolation 
on the
$s value so that sprintf($tmp[0], $s) turns into sprintf(tmp[0], 
$array[0],
$array[1]).  I'm starting to get the feeling that this will be 
impossible.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search  Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sprintf and arrays.

2005-04-06 Thread Richard Lynch
On Wed, April 6, 2005 4:23 am, Duncan Hill said:
 I have a snippet of code that looks something like:
 if (is_array($p_sub_values)) {
 foreach ($p_sub_values as $i = $v) {
  $p_sub_values_str[$i] = '$v';
 }
 $s = join(',', $p_sub_values_str);
 $r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
 }

 $tmp[0] in this case contains a string like 'Fred likes %1$s on his %2$s',
 taking advantage of positional substitution with sprintf.

 The function call to this snippet can have an optional array passed.  My
 need/desire is to substitute each element of the array into the
 appropriate
 position with sprintf.  So far I've tried:
 $r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
 $r = htmlentities(sprintf($tmp[0], ${$s}, ENT_QUOTES);

As far as I know, there's no super easy way to do what you want for an
arbitrary number of arguments...

While PHP does have support for functions to take an arbitrary number of
arguments, there's no way to pass those on down to something else, like $@
in shell (and Perl?).  Least not as far as I know.

 and a few other bits and pieces, all to no avail (error is about not
 enough
 arguments).

 Is there any way to accomplish this in PHP, or do I need to roll my own
 substitution code?  The array can obviously be anything from a single
 value
 to 'unlimited' (though in practice will probably be less than 5).

You could, of course, do something like:

list($a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10) = $tmp;
sprintf($a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9, $a10);

Depending on your error reporting, you'll get messages, so you'll need to
use @ to suppress them.

I guess you could use count() and do string manipulation to build the
sprintf(...) you want and then use eval() and you could then avoid the
warnings...

Aha!

Wait a minute.

I *THINK* this will do what you want:

call_user_func_array('sprintf', $tmp);

Only I'm not sure you can use a built-in function for it...

http://php.net/call_user_func_array

Never used it, so not really sure it fits the bill.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sprintf?

2005-03-29 Thread René Fournier
I need to go from
1754210747 (decimal)
to
17 54 21 a7 47 (hex per character)
Basically, reformat the string for each character, converting it to 
Hex, and spacing after each two characters. Sprintf?

...Rene
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] sprintf troubles

2004-03-26 Thread Chris Thomas
Hey,
Im trying to use sprintf to format a number and left pad it with spaces, but
it doesnt want to add the spaces
I can pad it with anyother char using   sprintf(%'_8.2f, $val)  which will
left-pad the number with underscores

Has anyone had any luck padding with spaces??
or is there a better way to do this??

Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sprintf troubles

2004-03-26 Thread Tom Rogers
Hi,

Saturday, March 27, 2004, 1:31:33 AM, you wrote:
CT Hey,
CT Im trying to use sprintf to format a number and left pad it with spaces, but
CT it doesnt want to add the spaces
CT I can pad it with anyother char using   sprintf(%'_8.2f, $val)  which will
CT left-pad the number with underscores

CT Has anyone had any luck padding with spaces??
CT or is there a better way to do this??

CT Chris


If you are looking at it on a web page the spaces will be ignored
unless you stick it in a pre/pre block or convert the spaces to
nbs;

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sprintf

2002-11-07 Thread Tyler BIRD
hey I'm trying to make dynamic database queries using
sprintf
any way I can get around this error?

$sql = SELECT * FROM Vacation where StartDate LIKE '%s/ .
'\%' . /%s';
$sql = sprintf($sql,  $_GET[month],  $_GET[year] );

I want just a literal slash between the month and year,
printf seems to complain of to few
arguments I've tried escaping the meaning of the % in the
example above and still
get errors

Thanks
TYler

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] sprintf

2002-11-07 Thread Robert Cummings
$sql = SELECT * FROM Vacation where StartDate LIKE '%s\\%s';

That should work :)

Cheers,
Rob.

Tyler BIRD wrote:
 
 hey I'm trying to make dynamic database queries using
 sprintf
 any way I can get around this error?
 
 $sql = SELECT * FROM Vacation where StartDate LIKE '%s/ .
 '\%' . /%s';
 $sql = sprintf($sql,  $_GET[month],  $_GET[year] );
 
 I want just a literal slash between the month and year,
 printf seems to complain of to few
 arguments I've tried escaping the meaning of the % in the
 example above and still
 get errors
 
 Thanks
 TYler
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php

-- 
.-.
| Robert Cummings |
:-`.
| Webdeployer - Chief PHP and Java Programmer  |
:--:
| Mail  : mailto:robert.cummings;webmotion.com |
| Phone : (613) 731-4046 x.109 |
:--:
| Website : http://www.webmotion.com   |
| Fax : (613) 260-9545 |
`--'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] sprintf() question

2002-08-29 Thread SpamSucks86

I read the manual page on sprintf, but I still don't understand exactly
how the format string works. 

 

$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output 123.1;
$formatted = sprintf(%01.2f, $money);
// echo $formatted will output 123.10

 

How does that work? What part of the string corresponds to what? Thanks
for any and all help!




Re: [PHP] sprintf() question

2002-08-29 Thread @ Edwin
Experiment a bit and I'm sure you'll find it easy. One example, change this 
portion

  $formatted = sprintf("%01.2f", $money);

to

  $formatted = sprintf("%01.3f", $money);

then see how it works...

?php

$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
echo "This is money: $money";
$formatted = sprintf("%01.3f", $money);
echo "This is formatted: $formatted";

?

HTH,

- E


I read the manual page on sprintf, but I still don't understand exactly
how the format string works.



$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"



How does that work? What part of the string corresponds to what? Thanks
for any and all help!





_
$B%-%c%j%"%"%C%W$rL\;X$9$"$J$?$N%J%S%2!<%?!<(B MSN $B="?&!>?&(B 
http://career.msn.co.jp/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] sprintf precision modifiers

2002-03-05 Thread Erik Price

The manual for sprintf

http://www.php.net/manual/en/function.sprintf.php

gives a nice introduction to the idea of the different formatting 
options.  But, the reader is referred to the examples to see the 
different ways that you can format a number.  Is there a better resource 
than this for formatting codes in sprintf ?  I think that the 
annotations are great, but you shouldn't have to rely on these 
afterthoughts for your information (I didn't see how I could limit the 
trailing zeroes until I got to the annotations).

Something tells me that for C programmers, formatting output like this 
is old hat, but for those of us who are just getting our feet wet in 
programming, there should be a more exhaustive reference as to what can 
be done... is there any such reference?

Thanks,
Erik







Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] sprintf()'s in if()'s

2001-05-04 Thread Chris Cameron

I suppose when you have so few problems you get to nitpick..

Guess my question is; why doesn't this work?

if(sprintf(%01.2f,$Result[CurrentPrice]+$Result[Increment]) = 
sprintf(%01.2f,$BidAmt)) {

when this works;
if(0.30 = 1.00) {

I'll just settle with a workaround for now.

Thanks,
Chris


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] sprintf()'s in if()'s

2001-05-04 Thread Jason Brooke

 I suppose when you have so few problems you get to nitpick..

 Guess my question is; why doesn't this work?

 if(sprintf(%01.2f,$Result[CurrentPrice]+$Result[Increment]) =
sprintf(%01.2f,$BidAmt)) {

 when this works;
 if(0.30 = 1.00) {

 I'll just settle with a workaround for now.

 Thanks,
 Chris

The former is being treated as a string, the latter is a double/float
You probably don't need to format them when making the comparison. Just try:

if ( ($Result[CurrentPrice]+$Result[Increment]) = $BidAmt) {

jason




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]