Formatting a number [message #180206] |
Tue, 29 January 2013 04:45 |
Scott Johnson
Messages: 196 Registered: January 2012
Karma: 0
|
Senior Member |
|
|
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 10:19 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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 10:20 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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 10:38 |
Paul Herber
Messages: 26 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
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 10:40 |
Paul Herber
Messages: 26 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
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 13:28 |
Scott Johnson
Messages: 196 Registered: January 2012
Karma: 0
|
Senior Member |
|
|
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 14:39 |
Richard Yates
Messages: 86 Registered: September 2013
Karma: 0
|
Member |
|
|
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 10:05 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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 15:33 |
Richard Yates
Messages: 86 Registered: September 2013
Karma: 0
|
Member |
|
|
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] |
Thu, 31 January 2013 00:38 |
Scott Johnson
Messages: 196 Registered: January 2012
Karma: 0
|
Senior Member |
|
|
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
|
|
|