Link to home
Start Free TrialLog in
Avatar of eduardo12fox
eduardo12foxFlag for Brazil

asked on

Erro format currency

Hello guys,

I have a TEdit and on event Onchange I created a function for format in real time my currency but I get erro:  'R$ 1,00' is not a valid floating point value.



procedure TF_produtos.Edit4Change(Sender: TObject);

 var Resultado: Real;
begin

      Resultado:= StrToFloat(Edit4.Text);
      Edit4.Text:= FormatFloat('R$ ###,###,##0.00',Resultado);

end;

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try this instead

Edit4.Text:= formatfloat('"R$ "###,###,##0.00', Resultado);

Open in new window

you are not giving the function a specific format
> So the function uses the default format defined for your user in windows (setting in control panel country settings)

does your user setting match the format you want ?
lol, i tested your idea ...
i'd probably get very frustrated with a solution like that
procedure TuMain.Edit1Change(Sender: TObject);
var x: Double;
  IsOk: Boolean;
begin
  IsOk := True;
  try
    x := StrToFloat(TEdit(Sender).Text);
  except
    on e: Exception do
      IsOk := False;
  end;
  if IsOk then
    TEdit(Sender).Text:= FormatFloat('R$ ###,###,##0.00', X);
end;

Open in new window


after your second edit ... the string holds your currecy symbol which is interpreted too by the StrToFloat

you need a real currency edit component

a currency edit component has a SEPARATE edit for the value
and a currency symbol
you could place a label with the currency in front of the edit

searching on torry.net wasn't very satisfactory for such a component

devexpress doesn't have a very good solution either
they have an edit format and a display format > no choice for the currency

an edit only has 1 format
and if you change the edited text while someone is typing, you'll get some very unexpected results

on page 2 on my search i found a few currency edit components
http://torry.net/quicksearchd.php?String=currency&page=2
Avatar of eduardo12fox

ASKER

Erro in all... I not need that symbol. I need format in real time.
My windows is set up correctly

I try that but the cursor is wrong
OnKeyPress
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); 
begin 
if NOT (Key in ['0'..'9', #8, #9]) then 
key := #0; 
//Função para posicionar o cursor sempre na direita 
Edit1.selstart := Length(Edit1.text); 
end;

Open in new window



and onChange

procedure TForm1.Edit1Change(Sender: TObject); 
var 
s : string; 
v : double; 
I : integer; 
begin 
//1º Passo : se o edit estiver vazio, nada pode ser feito. 
If (edit1.Text = emptystr) then 
Edit1.Text := '0,00'; 

//2º Passo : obter o texto do edit, SEM a virgula e SEM o ponto decimal: 
s := ''; 
for I := 1 to length(edit1.Text) do 
if (edit1.text[I] in ['0'..'9']) then 
s := s + edit1.text[I]; 

//3º Passo : fazer com que o conteúdo do edit apresente 2 casas decimais: 
v := strtofloat(s); 
v := (v /100); // para criar 2 casa decimais 

//4º Passo : Formata o valor de (V) para aceitar valores do tipo 0,10. 
edit1.text := FormatFloat('###,##0.00',v); 
edit1.SelStart := 0; 
end; 

Open in new window



when I test not do correcty
first steps in Delphi ?

you don't like a decimal separator ?
if NOT (Key in ['0'..'9', #8, #9]) then 

Open in new window


what about a thousand separator ?

you have a constant for an emptystr ??????
use 2 single quotes
If (edit1.Text = '') then 

Open in new window


this is not C, C#, C++ or whatever languages which ( requires ) all ( those ) extra {[( round () brackets )]}
for 1 comparison there is no point in kneebending your fingers to type those 2 extra ()
If  edit1.Text = '' then 

Open in new window


this will never work correctly
validation of an entered value happens before the focus is changed (or enter is pressed)

have you considered someone copy/pasting a piece of text into your edit ?

let the user type, and validate it afterwards
preventing key strokes is a nice to have, but you'll still have to validate the entry

the combination will never work
My code is Done! The problem is when I will empty field aways I not can delete the last two number for example:

1.254,00

when I will delete this number in the field I use the button Delete of keyboard. But it delete some number the two last number not delete.
as i said the combination of validating and changing the text in the onchange event will never work

you will remember that after you get complaints from the users
you don't have to believe me
just wait and see

fwiw

#8 = backspace
the delete of the numeric keyboard is a virtual key stroke: VK_DELETE

you didn't implement VK_DELETE actions
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.