Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 9 of 9 total
Thread Showing thousands and even currency values
Sat, Aug 15 2015 7:30 PMPermanent Link

Trinione

How can 12900.50 be displayed as 12,900.50 or even $12,900.50
Sat, Aug 15 2015 8:38 PMPermanent Link

Trinione

Just created this and sharing.

Wondering if AsCurrency and a FloatToCurrency function can be included?
---------------------------------------------------------

function AddThousandSeparator(S: string): string;
var  
 i, j, len: Integer;
 sl: array of string;

begin               
 j := 0;
 sl := split(s, '');
 len := Length(sl) - 3;
 for i := len downto 0 do
 begin              
   if j Mod 3 = 0 then
     if i <> len then
       Insert(',', sl, i);
   Inc(j);
 end;
 result := join(sl);
end;

function GetAsCurrency(s: string): string;
begin
 result := '$' + AddThousandSeparator(s);
end;
Sun, Aug 16 2015 3:24 AMPermanent Link

Malcolm Taylor

Hi

The function should also take care of decimals and use FormatSettings
so that non-US currency and separators are used where appropriate.  Surprised
Sun, Aug 16 2015 11:14 AMPermanent Link

Trinione

<< The function should also take care of decimals and use FormatSettings
so that non-US currency and separators are used where appropriate.  Surprised >>

Actually, I had already modified the code to cater for amounts less than $1.00 as in $0.50.

However, I am not seeing any help with FormatSettings that can do what you specified.

Can you give an example code using the FormatSettings so that non-US currency and separators are used please.

---------------------------------------------------------------------------
function AddThousandSeparator(S: string): string;
var  
 i, j, len: Integer;
 sl: array of string;

begin               
 j := 0;
 sl := split(s, '');

 if Length(sl) = 3 then
   Insert('0', sl, 0)
 else
 begin
   len := Length(sl) - 3;
   for i := len downto 0 do
   begin
     if j Mod 3 = 0 then
       if i <> len then
         Insert(',', sl, i);
     Inc(j);
   end;
 end;
 result := join(sl);
end;
Sun, Aug 16 2015 2:34 PMPermanent Link

Malcolm Taylor

Sure.

First I assume your Split and Join were intended to separate then
append the decimal fraction.  I imagine your code actually specifies
the separator as '.' unlike your post.  Smiley

The Locale handling is a bit academic at present as Tim has confirmed
that EWB hard codes it to US English at present.  However, he has also
said Locale detection is on his 'list'.

As a developer you can set your own FormatSettings if you are targeting
a specific Locale, or you could offer users an option to set their own
ones - or you can stay with US English.

To future-proof your function, I think that all you have to do is
replace '.' and ',' with:
 -  FormatSettings.DecimalSeparator
 -  FormatSettings.ThousandsSeparator

for example:  
  sl :=  Split(s, FormatSettings,DecimalSeparator);  // similar for
Join

On trying it, I don't see the usual (Delphi) 'CurrencyFormat' and
'CurrencyString' defines but I have not looked at the source to see if
they are there under different names.  So I don't know if you can
future-proof the currency symbol and its placement at present.  Frown

Hopefully, Tim will be providing the function you have requested, in
due course.
Sun, Aug 16 2015 3:07 PMPermanent Link

Malcolm Taylor

Uh-oh!
Looks like the ThousandSeparator does not exist either.  Just the
DecimalSeparator.  Frown
Mon, Aug 17 2015 9:10 AMPermanent Link

Trinione

Ha ha. You have seen how I ended up where I ended up. Smile

As it is I only need be concerned about $ values, so the function I wrote (Ver 2 of it), handles it perfectly.

I would expect Currency and Money handling to be a part of EWB/EDB going forward though.
Mon, Aug 17 2015 12:57 PMPermanent Link

Matthew Jones

Malcolm wrote:

> However, he has also
> said Locale detection is on his 'list'.

I have to pick up on this and say "eek!". Apart from the use of
floating point for any currency use, particularly where calculations
are involved, the last thing I'd want would be the user's browser
messing with any numbers that are being displayed. I can sort of see
how the thousands separator and decimal make sense, but it isn't too
far to using the currency symbol, and then you fell off the edge of the
planet.

FWIW, I have always used an integer value of "pence" (or cents), and I
have the server do things like VAT calculations. Indeed, the order
processing checks that the "basket" data is all the same as the server
has, so that it can detect any attempts at adjusting prices in the
javascript.

I realise though that this may not matter so much in some situations,
but it should be considered.

--

Matthew Jones
Mon, Aug 17 2015 1:38 PMPermanent Link

Malcolm Taylor

Matthew Jones wrote:

>
> I have to pick up on this and say "eek!". Apart from the use of
> floating point for any currency use, particularly where calculations
> are involved, the last thing I'd want would be the user's browser
> messing with any numbers that are being displayed. I can sort of see
> how the thousands separator and decimal make sense, but it isn't too
> far to using the currency symbol, and then you fell off the edge of
> the planet.
>

Hi Matthew

I agree that it is a step too far to decorate floats beyond the decimal
separator for input purposes at present.  And I don't see it happening
on sites I visit/use.
But for the purposes of display I think it can have a place.

In the longer term I don't see an insurmountable problem with allowing
input in Locale formats.  But that requires EWB to complete the
FormatSettings, make the edit components Locale-aware, add Locale
detection (or selection) and ...

But I have no idea whether browsers and JS can cope or how hard it
would be for Tim, so I am not holding my breath.  Smile
Image