FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Formatting a number
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Formatting a number [message #180206] Mon, 28 January 2013 23:45 Go to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Hello

I am looking if there is a better way to write the following if possible.

I would like to display the negative sign in front of the dollar sign
when the unit is negative.

if($units < 0){
$f_units = '-$' . number_format($units * -1);
} else {
$f_units = '$' . number_format($units);
}

I need $units to stay untouched since I am using it in a calculation
later down.

Thanks
Scotty
Re: Formatting a number [message #180209 is a reply to message #180206] Tue, 29 January 2013 05:19 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Jan 29, 4:45 am, Scott Johnson <nooneh...@chalupasworld.com> wrote:
> Hello
>
> I am looking if there is a better way to write the following if possible.
>
> I would like to display the negative sign in front of the dollar sign
> when the unit is negative.
>
> if($units < 0){
>         $f_units = '-$' . number_format($units * -1);} else {
>
>         $f_units = '$' . number_format($units);
>
> }
>
> I need $units to stay untouched since I am using it in a calculation
> later down.
>
> Thanks
> Scotty

How do you define "better". I can give you "other" ways to do it, but
I have no idea what your criteria would be in deciding whether any of
them are better.

Here's one:
$f_units = (($units<0)?'-':'').number_format(abs($units));
Re: Formatting a number [message #180210 is a reply to message #180206] Tue, 29 January 2013 05:20 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 29/01/13 04:45, Scott Johnson wrote:
> Hello
>
> I am looking if there is a better way to write the following if possible.
>
> I would like to display the negative sign in front of the dollar sign
> when the unit is negative.
>
> if($units < 0){
> $f_units = '-$' . number_format($units * -1);
> } else {
> $f_units = '$' . number_format($units);
> }
>
> I need $units to stay untouched since I am using it in a calculation
> later down.
>
> Thanks
> Scotty
try printf or sprintf..



--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: Formatting a number [message #180212 is a reply to message #180206] Tue, 29 January 2013 05:38 Go to previous messageGo to next message
Paul Herber is currently offline  Paul Herber
Messages: 26
Registered: February 2011
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
On Mon, 28 Jan 2013 20:45:09 -0800, Scott Johnson <noonehome(at)chalupasworld(dot)com> wrote:

> Hello
>
> I am looking if there is a better way to write the following if possible.
>
> I would like to display the negative sign in front of the dollar sign
> when the unit is negative.
>
> if($units < 0){
> $f_units = '-$' . number_format($units * -1);
> } else {
> $f_units = '$' . number_format($units);
> }
>
> I need $units to stay untouched since I am using it in a calculation
> later down.

Have you looked at money_format()
Might be available on all systems though.



--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/ twitter: @sandrilaLtd
Re: Formatting a number [message #180213 is a reply to message #180212] Tue, 29 January 2013 05:40 Go to previous messageGo to next message
Paul Herber is currently offline  Paul Herber
Messages: 26
Registered: February 2011
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
On Tue, 29 Jan 2013 10:38:59 +0000, Paul Herber <paul(at)pherber(dot)com> wrote:

> On Mon, 28 Jan 2013 20:45:09 -0800, Scott Johnson <noonehome(at)chalupasworld(dot)com> wrote:
>
>> Hello
>>
>> I am looking if there is a better way to write the following if possible.
>>
>> I would like to display the negative sign in front of the dollar sign
>> when the unit is negative.
>>
>> if($units < 0){
>> $f_units = '-$' . number_format($units * -1);
>> } else {
>> $f_units = '$' . number_format($units);
>> }
>>
>> I need $units to stay untouched since I am using it in a calculation
>> later down.
>
> Have you looked at money_format()
> Might be available on all systems though.
^ not


--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/ twitter: @sandrilaLtd
Re: Formatting a number [message #180216 is a reply to message #180209] Tue, 29 January 2013 08:28 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 1/29/2013 2:19 AM, Captain Paralytic wrote:
> On Jan 29, 4:45 am, Scott Johnson <nooneh...@chalupasworld.com> wrote:
>> Hello
>>
>> I am looking if there is a better way to write the following if possible.
>>
>> I would like to display the negative sign in front of the dollar sign
>> when the unit is negative.
>>
>> if($units < 0){
>> $f_units = '-$' . number_format($units * -1);} else {
>>
>> $f_units = '$' . number_format($units);
>>
>> }
>>
>> I need $units to stay untouched since I am using it in a calculation
>> later down.
>>
>> Thanks
>> Scotty
>
> How do you define "better". I can give you "other" ways to do it, but
> I have no idea what your criteria would be in deciding whether any of
> them are better.
>
> Here's one:
> $f_units = (($units<0)?'-':'').number_format(abs($units));
>

I originally wrote 'elegant' but I think you got the jest.

I like one liners as yours and forgot about the abs() function
Thanks.
Re: Formatting a number [message #180217 is a reply to message #180209] Tue, 29 January 2013 09:39 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
On Tue, 29 Jan 2013 02:19:14 -0800 (PST), Captain Paralytic
<paul_lautman(at)yahoo(dot)com> wrote:

> On Jan 29, 4:45 am, Scott Johnson <nooneh...@chalupasworld.com> wrote:
>> Hello
>>
>> I am looking if there is a better way to write the following if possible.
>>
>> I would like to display the negative sign in front of the dollar sign
>> when the unit is negative.
>>
>> if($units < 0){
>>         $f_units = '-$' . number_format($units * -1);} else {
>>
>>         $f_units = '$' . number_format($units);
>>
>> }
>>
>> I need $units to stay untouched since I am using it in a calculation
>> later down.
>>
>> Thanks
>> Scotty
>
> How do you define "better". I can give you "other" ways to do it, but
> I have no idea what your criteria would be in deciding whether any of
> them are better.
>
> Here's one:
> $f_units = (($units<0)?'-':'').number_format(abs($units));

I think you mean:
$f_units = (($units<0)?'-$':'$').number_format(abs($units));
Re: Formatting a number [message #180218 is a reply to message #180217] Wed, 30 January 2013 05:05 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Jan 29, 2:39 pm, Richard Yates <rich...@yatesguitar.com> wrote:
> On Tue, 29 Jan 2013 02:19:14 -0800 (PST), Captain Paralytic
>
>
>
>
>
>
>
>
>
> <paul_laut...@yahoo.com> wrote:
>> On Jan 29, 4:45 am, Scott Johnson <nooneh...@chalupasworld.com> wrote:
>>> Hello
>
>>> I am looking if there is a better way to write the following if possible.
>
>>> I would like to display the negative sign in front of the dollar sign
>>> when the unit is negative.
>
>>> if($units < 0){
>>>         $f_units = '-$' . number_format($units * -1);} else {
>
>>>         $f_units = '$' . number_format($units);
>
>>> }
>
>>> I need $units to stay untouched since I am using it in a calculation
>>> later down.
>
>>> Thanks
>>> Scotty
>
>> How do you define "better". I can give you "other" ways to do it, but
>> I have no idea what your criteria would be in deciding whether any of
>> them are better.
>
>> Here's one:
>> $f_units = (($units<0)?'-':'').number_format(abs($units));
>
> I think you mean:
> $f_units = (($units<0)?'-$':'$').number_format(abs($units));

Or I may have meant:
$f_units = (($units<0)?'-':'').'$'.number_format(abs($units));
Re: Formatting a number [message #180219 is a reply to message #180218] Wed, 30 January 2013 10:33 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
On Wed, 30 Jan 2013 02:05:01 -0800 (PST), Captain Paralytic
<paul_lautman(at)yahoo(dot)com> wrote:

> On Jan 29, 2:39 pm, Richard Yates <rich...@yatesguitar.com> wrote:
>> On Tue, 29 Jan 2013 02:19:14 -0800 (PST), Captain Paralytic
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> <paul_laut...@yahoo.com> wrote:
>>> On Jan 29, 4:45 am, Scott Johnson <nooneh...@chalupasworld.com> wrote:
>>>> Hello
>>
>>>> I am looking if there is a better way to write the following if possible.
>>
>>>> I would like to display the negative sign in front of the dollar sign
>>>> when the unit is negative.
>>
>>>> if($units < 0){
>>>>         $f_units = '-$' . number_format($units * -1);} else {
>>
>>>>         $f_units = '$' . number_format($units);
>>
>>>> }
>>
>>>> I need $units to stay untouched since I am using it in a calculation
>>>> later down.
>>
>>>> Thanks
>>>> Scotty
>>
>>> How do you define "better". I can give you "other" ways to do it, but
>>> I have no idea what your criteria would be in deciding whether any of
>>> them are better.
>>
>>> Here's one:
>>> $f_units = (($units<0)?'-':'').number_format(abs($units));
>>
>> I think you mean:
>> $f_units = (($units<0)?'-$':'$').number_format(abs($units));
>
> Or I may have meant:
> $f_units = (($units<0)?'-':'').'$'.number_format(abs($units));

Yes, I thought of that one and wondered if there is any difference in
their efficiency, however small. I went with the first only because it
was fewer characters.
Re: Formatting a number [message #180220 is a reply to message #180218] Wed, 30 January 2013 19:38 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 1/30/2013 2:05 AM, Captain Paralytic wrote:
> On Jan 29, 2:39 pm, Richard Yates <rich...@yatesguitar.com> wrote:
>> On Tue, 29 Jan 2013 02:19:14 -0800 (PST), Captain Paralytic
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> <paul_laut...@yahoo.com> wrote:
>>> On Jan 29, 4:45 am, Scott Johnson <nooneh...@chalupasworld.com> wrote:
>>>> Hello
>>
>>>> I am looking if there is a better way to write the following if possible.
>>
>>>> I would like to display the negative sign in front of the dollar sign
>>>> when the unit is negative.
>>
>>>> if($units < 0){
>>>> $f_units = '-$' . number_format($units * -1);} else {
>>
>>>> $f_units = '$' . number_format($units);
>>
>>>> }
>>
>>>> I need $units to stay untouched since I am using it in a calculation
>>>> later down.
>>
>>>> Thanks
>>>> Scotty
>>
>>> How do you define "better". I can give you "other" ways to do it, but
>>> I have no idea what your criteria would be in deciding whether any of
>>> them are better.
>>
>>> Here's one:
>>> $f_units = (($units<0)?'-':'').number_format(abs($units));
>>
>> I think you mean:
>> $f_units = (($units<0)?'-$':'$').number_format(abs($units));
>
> Or I may have meant:
> $f_units = (($units<0)?'-':'').'$'.number_format(abs($units));
>

Yes I like this one, tks. @cp
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: PEAR DB: "PHP Fatal error: Call to undefined method DB::query()"
Next Topic: cURL
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Oct 19 06:57:03 EDT 2017

Total time taken to generate the page: 0.00831 seconds