Solved

StrToCurr and DecimalSeparator

Posted on 2002-07-11
11
1,692 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now