How to use Delphi's AsCurrency to show currency without currency symbol? - delphi

I'm trying to display a currency value in a grid, but I do not want the currency symbol to be shown:
if fPreferences.WorksheetFormat = 'Numeric' then
begin
CurrencyString := '';
Value := FieldByName('UnitList').AsCurrency;
end else
Value := CurrToStrF(FieldByName('UnitList').AsCurrency, ffCurrency, 2, langFormat);
The problem is that it's still showing the currency symbol. What am I doing wrong here? I don't think I can use CurrToStrF, because I need the grid to export a number to excel, not a string. Or, is there any way I can use AsFloat, but have to decimal places? (100.00)

Doing CurrencyString := ''; will impact all the following formatting of currencies when using the default format strings and thus should display all the currency variants/fields values without the $ sign, while retaining their numeric nature.
But when you explicitly format your currency value with your own TFormatSettings langFormat, it has no effect unless you previously did:
langFormat.CurrencyString := '';

Changing ffCurrency to ffFixed should get rid of the currency symbol but there wouldn't be any hundreds separators.
//With separators
sStrVar := FormatCurr('#,##0.00', CurrVar);

a very simple solution would be to change the CurrencyString yourself
and change it back to the original value later.
if fPreferences.WorksheetFormat = 'Numeric' then
begin
CurrencyString := '';
Value := FieldByName('UnitList').AsCurrency;
end else
begin
OldCurrStr := CurrencyString;
CurrencyString := '';
Value := CurrToStrF(FieldByName('UnitList').AsCurrency, ffCurrency, 2, langFormat);
CurrencyString := OldCurrStr;
end;

Related

Am I able to use a logic function (and, or) between 2 comparisons

I am unsure whether it is generally impossible to use a logic function between to comparisons or if I've used my logic statement incorrectly because when I make all variables (NewUsername, NewUsername2, NewPass, NewPass2) to the characters "hi", it would continue to display the Application.MessageBox.
procedure TNewUserFrm.ApplyBtnClick(Sender: TObject);
begin
if (NewUsername <> NewUsername2) or (NewPass <> NewPass2) then
begin
Application.MessageBox('The usernames or passwords do not match. Try again', 'Error');
end
else
begin
if not modFile.UsersDataSet.Active then modFile.UsersDataSet.Open;
modFile.UsersDataSet.Append;
modFile.UsersDataSet.FieldByName('Username').AsString := NewUsername.Text;
modFile.UsersDataSet.FieldByName('Password').AsString := NewPass.Text;
modFile.UsersDataSet.Post;
NewUserFrm.Hide;
end;
NewUsername.Text := '';
NewUsername2.Text := '';
NewPass.Text := '';
NewPass2.Text := '';
ApplyBtn.SetFocus;
end;
I have tried using "and" statement, "or" statement and I've also tried using nested "if" statements instead but the same result occurs
You are comparing the TEdit controls addresses, not their content. You need to compare their contents.
if (NewUsername.Text <> NewUsername2.Text) or (NewPass.Text <> NewPass2.Text) then
Writing something like
NewUsername <> NewUsername2
will always have the value true in this case because these are two different TEdit controls, and their addresses will never be the same.

Delphi memo lines in one line

In a TMemo field I have 3 lines:
line1
line2
line3
Is it posible to get all three lines as one string?
Example:
line1,line2,line3
You can use the Lines.CommaText property for this. Do the following:
CommaString := Memo1.Lines.CommaText;
Its also useful to use the DelimitedText property if you want the text to make use of another separator character. You can do that by using something like this:
Memo1.Lines.Delimiter := '-';
Memo1.Lines.StrictDelimiter := True;
DashString := Memo1.Lines.DelimitedText;
This works both ways. You can assign a value to the CommaText or DelimiterText to set the lines. This is actually a of TStringList so it will work with TListBox, TMemo, TComboBox, etc. Basically anything that uses a string list internally.
maybe something like this suits your needs
d:=memo1.lines.count;
for i:=1 to d do
memo1.lines[0]:=memo1.lines[0]+' '+memo1.lines[i];
for i:=1 to d do
memo1.lines.Delete(1);
here is a 3 line function that do it.
function getOneLineMemo(memo:Tmemo):String;
var
i:integer;
begin
result := '';
for i:=0 to memo1.lines.count do
result := result + memo.lines[0];
end;

Indexes order in Delphi?

I'm basically coding some sort of table where for column tags I have some numbers and for row tags I have some strings which contain such numbers separated by commas.
I'm taking all the row tags from a TString named minterms_essentials and the column tags from one named minterms.
First I must tag the created 2 dimensions array. And then, if any string from the rows contains certain letter, I must place an 'x' in the proper column.
I've wrote this Delphi code but I'm getting access violation so far...
SetLength(tabla, minterms_essentials.Count+1,minterms.Count+1);
for i := 0 to minterms.Count-1 do
begin
tabla[0,i+1] := IntToStr(BinToInt(minterms[i]));
end;
for i := 0 to minterms_essentials.Count-1 do
begin
tabla[i+1,0] := minterms_essentials[i];
end;
for i := 1 to minterms_essentials.Count-1 do
begin
for g := 1 to minterms.Count-1 do
begin
ss := tabla[g,0].Split([',']);
for s in ss do
begin
if s = tabla[0,g] then
begin
tabla[g,i] := 'x';
end;
end;
end;
end;
Is there any better and correct way to do this?
Look at this:
first dimension is defined by minterms_essentials
SetLength(tabla, minterms_essentials.Count+1,minterms.Count+1);
but here you are using minterms to index first dimension of array:
for g := 1 to minterms.Count-1 do
begin
ss := tabla[g,0].Split([',']);
P.S. Have you still not turned on range check?

How to avoid "Type mismatch in expression" in ClientDataSet Filter

The erro msg "Type mismatch in expression" appear when I run this code:
CDSIndicados.Filtered := False;
CDSIndicados.Filter := 'EDICOES_ID like ' + QuotedStr(IntToStr(Integer(cxComboBox1.Properties.Items.Objects[cxComboBox1.ItemIndex])));
CDSIndicados.Filtered := True;
I know this message may appear when there is an error in the data type of the field. But I could not fix. Was that it?
I'm suspecting that your EDICOES_ID field is an integer value, in which case you don't need to quote it in your filter expression and the LIKE operator isn't supported AFAIK. If it is a string field, you do need the quotes and LIKE is supported, but you typically want a wildcard in the expression as well. (LIKE is only supported for character (string) type fields. For numerics or dates, you need to use the usual comparison operators >, <, >=, <=, = or BETWEEN.)
Do yourself a favor, too, and declare a local variable, and making sure that there's actually an item selected in the ComboBox before trying to access its Objects. I've added one both for the ItemIndex and for intermediate storage of the typecast Object you're retrieving, which makes it much easier to debug if you need to do so.
Here's a solution either way (whether it's an integer field, or a string that needs quoting).
var
Idx, Value: Integer;
begin
Idx := ComboBox1.ItemIndex;
if Idx > -1 then
begin
CDSIndicados.Filtered := False;
Value := Integer(cxComboBox1.Properties.Items.Objects[Idx]);
// If the field is an integer, you don't need a quoted value,
// and LIKE isn't supported in the filter.
CDSIndicados.Filter := 'EDICOES_ID = ' + IntToStr(Value);
// Not relevant here, but LIKE isn't supported for date values
// either. For those, use something like this
CDSIndicados.Filter := 'EDICOES_DATE = ' + QuotedStr(DateToStr(Value));
// or, if the field is string and you want LIKE, you need to
// quote the value and include a wildcard inside that quoted
// string.
CDSIndicados.Filter := 'EDICOES_ID LIKE ' + QuotedStr(IntToStr(Value) + '%');
CDSIndicados.Filtered := True;
end;
end;

How to convert int to currency?

I'm working with Delphi 2009,I binged my question,but the answers I've gotten are outdated since It doesn't recognise StrtoFloat in Delphi2009.
I'm asking how to convert an integer ,for example, '1900000' to '1,900,000'?
You can also use the format command. Because the format expects a real number, adding 0.0 to the integer effectively turns it into an extended type.
Result := Format('%.0m',[intValue + 0.0]));
This handles negative numbers properly and adds the currency symbol for the users locale. If the currency symbol is not wanted, then set CurrencyString := ''; before the call, and restore it afterwards.
SavedCurrency := CurrencyString;
try
CurrencyString := '';
Result := Format('%.0m',[intValue + 0.0]));
finally
CurrencyString := SavedCurrency;
end;
To force commas, just set the ThousandSeparator := ',';
CurrencyString := '!';
ThousandSeparator := '*';
Result := Format('%.0m',[-1900000.0]);
// Returns (!1*900*000) in my locale.
The "period" in the mask determines how the fractional portion of the float will display. Since I passed 0 afterwards, it is telling the format command to not include any fractional pieces. a format command of Format('%.3m',[4.0]) would return $4.000.
I currently use this :
function FloatToCurrency(const f: double): string;
begin
Result := FormatFloat('#,###.##;1;0', f);
end;
It doesn't work with negative numbers, but since you need currency you won't have that problem.
You can assign Integer to Currency directly by assignment, the compiler will do the conversion for you:
var
Int : Integer;
Cur : Currency;
begin
Int := 1900000;
Cur := Int;
ShowMessage(CurrToStr(Cur)); // 1900000
ShowMessage(Format('%m', [Cur]); // 1,900,000.00 in US/UK/NZ/AU etc, "1 900 000,00" in Spain etc.
ShowMessage(Format('%.0m', [Cur]); // 1,900,000 in US/UK/NZ/AU etc, "1 900 000" in Spain etc.
end;
If you want Commas using Spanish regional settings set ThousandSeparator := ','; or use the extended CurrToStrF(amount, ffCurrency, decimals, FormatSettings)) version.
The verison with FormatSettings is also thread-safe.
Note: You can't assign Currency to Integer directly, You would need to use Int := Trunc(Cur) but this is inefficient as it converts to float first (unless compiler does something smart).
wouldnt this be more of a format thing, delphi should have some type of support for formating the number into a string the way you want right? Besides isnt the newer versions of delphi more aligned with the .net framework?

Resources