• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

To set Format a numberic string value


Dear advisor !

I use Delphi 2005

I want to update vlue from a Table on nuber field. I read value from number field to a Tedit. I use IntToStr() . But i want to format value like '123,456,789' . And after the user modify value , and want to covert it like '123456'. Because the SQL Command does not accept 'commas' on value.

Please show me the function or make best function for me.

what i ask : two functions. the fisrt to format value with separate charater like from '123456' to '123,456' . The second funtion to make '123,456' to '123456'

Thank for all consider

0
namcit99
Asked:
namcit99
  • 6
  • 3
  • 2
1 Solution
 
TheRealLokiSenior DeveloperCommented:
   var
        f: double;
    begin
        edit1.text := formatfloat('#,###', 123456); // returns "123,456"

        f := StrToFloat( StringReplace(edit1.text, ',', '', [rfReplaceAll])); // returns 123456

        edit2.text := floattostr(f);
    end;
0
 
namcit99Author Commented:

Your solution is good.

But there is only one unconvenient. If the number value is 0, the Formatfloat() function returns '' (empty charater). I need when 0, return '0' . Whether formatFloat does it well. show me the best way to solve exspecial case
0
 
namcit99Author Commented:

whether Delphi has function like IIF function in Visual FoxPro

IIF(lExpression, eExpression1, eExpression2)

if lExpression is True then it returns eExpression1. Orther case it returns eExpression2
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
namcit99Author Commented:

lcdaung   :=  Formatfloat('#,###', Dkdmnv.ADOQrydmnv.FieldByName('Daung').AsInteger)    ;

updatedmnv.txtdaung.Text    := IFThen(lcdaung='', '0', lcdaung)   ;

Any more good way ? Too long command
0
 
namcit99Author Commented:

I should write two command to get the lastest result
0
 
mikelittlewoodCommented:
Integer to string

FloatToStrF(iTemp, ffNumber, 9, 0)  // just change the 9 part to a large enough number for the values you pass in
0
 
mikelittlewoodCommented:
For the string to integer I would part of your question I would use Loki's answer of

StrToFloat( StringReplace(edit1.text, ',', '', [rfReplaceAll])); // returns 123456
0
 
TheRealLokiSenior DeveloperCommented:
change it to be
 edit1.text := formatfloat('#,##0', 123456); // returns "123,456"
the '0' acts as a placeholder and will be a zero instead of ''
0
 
namcit99Author Commented:

Two ways are good. I want to accept two comment. But i could not find out "Split" link
0
 
mikelittlewoodCommented:
When you accept an answer, the next screen allows you to split the points and choose more people.
0
 
namcit99Author Commented:

Thank for all consider
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now