Solved

StrToCurr and DecimalSeparator

Posted on 2002-07-11
11
1,752 Views
Last Modified: 2010-05-18
Hi,

Do you know what the DecimalSeparator value would be by default for people running a Delphi 4 program in any of these countries?  Do they have a value different from "."?

Mexico
Sweden
Italy
Spain

The reason I ask is that I am using StrToCurr to convert strings to currency values.  All of the strings I am passing to StrToCurr have a "." as the decimal separator.

Now I just realised that if someone runs my program in a country where they have a DecimalSeparator *other* than a ".", they will get an exception.  So I assume I would need to add DecimalSeparator := '.'; at the start of my program to make it work for all users.

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
  • 2
  • 2
  • 2
  • +3
11 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7147281
use the GetNumberformat API function to format your strings prior to passing them to StrToCurr and you'll be fine.



Good luck!!
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7147316
in europe mostly the comma is the decimalseparator

why not do this
DecimalSeparator := '.';
allways, if you have problems with
other decimalseparators?

meikl ;-)
0
 
LVL 3

Expert Comment

by:marcoszorrilla
ID: 7147317
The trouble as I think is that the String can contain ',' or '.' because if its entered by keyboard you don't know what character will be introduced by the user, I'm spanish but if the user type . in the string.

I think that the trouble is to know if the decimal separator enter by keyboard is the same defined in Windows or not.

Perhaps something like this, but adapted at your to your necessities:

procedure TForm1.Button1Click(Sender: TObject);
var
dinero:String;
Pasta:Currency;
begin
dinero:='34,55';

if pos('.',dinero) > 0 then
DecimalSeparator:='.'
else
DecimalSeparator:=',';

Pasta:=StrToCurr(Dinero);
showMessage(CurrToStr(pasta));
end;


Best Regards
Marcos.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:DrDelphi
ID: 7147329
Here's another thought... use Delphi's own FormatFloat to determine which is the decimal seperator. For example:

 var
   DecSep:string;
 begin
   DecSep:=Copy( FloatToStr(1.1) ,2,1 );
   showmessage(DecSep);
 end;

-- here in the US this gives me '.' However, changing my Regional settings to a European local (Spain), it returns ','.... game , set, match!

Good luck!!

0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7147700
even easier :  

var
  Money : double;
begin
  Money := strtoCurr(edit2.text);
  edit1.Text := format('%m',[Money]);
end;


StrtoCurr and format both use the localisation settings,
so strtocurr('144,69') causes an exception in Australia, but works fine in Sweden (where they use ',')
likewise strtocurr('144.69') causes an exception in Sweden

This is exactly what you want, ie users _have_ to use their local money constants, and the output is always displayed in local format.

from the help : (format)

m     Money. The argument must be a floating-point value. The value is converted to a string that represents a currency amount. The conversion is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, DecimalSeparator, and CurrencyDecimals global variables, all of which are initialized from the Currency Format in the International section of the Windows Control Panel. If the format string contains a precision specifier, it overrides the value given by the CurrencyDecimals global variable.
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7147715
BTW, as far as I can tell the, Spanish Peseta has no decimal component, so there's no point parsing for it :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7148203
nevermore pesetas in spain->euro now
0
 
LVL 3

Expert Comment

by:marcoszorrilla
ID: 7148374
Very sorry about but as Kretzschmar say nevermore Pesetas in Spain but Euro.

Best Regards.
Marcos - From Spain.
0
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
ID: 7148822
Hi,
My suggestion is to write your own function like:

function Str2Curr(const St: string): Currency;
var
  i: integer;
  S: string;
begin
  S := St;
  if DecimalSeparator <> '.' then begin
    i := Pos('.', S);
    if i > 0 then begin
      Delete(S, i, 1);
      Insert(DecimalSeparator, S, i);
    end;
  end;
  result := StrToCurr(S);
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
var
  c: currency;
begin
  c := Str2Curr('1234.56');
  ShowMessage(FloatToStr(c));
end;

The problem occurs when your numbers come from code (like above) or are wrongly entered by the user (entered value doesn't match regional settings).

That function replacement eliminates both possible error sources.

Changing DecimalSeparator value is only applicable if you force your users to use '.' instead of their usual separator. (Which I wouldn't accept if I were the user)

Regards, Geo
0
 

Author Comment

by:Richard2000
ID: 7150084
Hi,

Many thanks for your comments.

I often use the currency datatype to store other, non-money data for my programs, such as a version number or size of a printer margin.  I use this over the floating point type because it avoids rounding errors.

Are commas used when displaying and entering numbers that have a decimal separator but *aren't* money values, in countries such as Sweden?

For example, if a user sees my About box in Sweden, should I display the version number as 1.3 or 1,3?

Another example, if a user enters a size of a printer margin in Sweden, would they enter it as 2.5 or 2,5?

Richard
0
 
LVL 17

Expert Comment

by:geobul
ID: 7150194
Hi Richard,

Numbers: Yes, comma can be used for entering numbers. Even more: pressing num keypad '.' is displayed as ',' by some programs. That is true if the decimal separator is comma, of course.

Version: You decide how it will be displayed. However, I think that version is not a real number actually and is usually displayed using periods (i.e.: 2.1.5).

Printer parameters: My experience shows that only 2.5 format is acceptable. It doesn't matter what the regional settings are.

Regards, Geo
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question