?
Solved

StrToCurr and DecimalSeparator

Posted on 2002-07-11
11
Medium Priority
?
1,837 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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