Board index » cppbuilder » Truncating decimals in use of Floats in a Filename

Truncating decimals in use of Floats in a Filename


2008-02-29 04:02:40 PM
cppbuilder50
Hi All
I am using this line of text to setup a default SAVE-As file name.
AnsiString TempName = "Wave_" + FloatToStr(Height) + "_" +
FloatToStr(Period) + "_" + FloatToStr(Location);
As you can see these values that are brought into the filename are
floats and not integers and sometimes contain one or more digits after
the decimal.
This works fine for 1 decimal floats, but often with two decimal floats,
and without formating, the default name could be as follows:
Wave_2.5_13.4499998092651_63.5400009155273.txt
How do I limit the decimals to 2, so as to retain neat filenames.
Normally I use
sprintf(tmpstr,"Wave_%4.2f_%4.1f_%5.2f",Height,Period,Location);
to format these floats to a string, but this method creates white spaces
infront of each number in the string, so does not work in a filename.
Any suggestions?
Thanks
Barry
 
 

Re:Truncating decimals in use of Floats in a Filename

I eventually went with
AnsiString TempName = "Wave_" + FloatToStrF(Height,ffFixed,7,2) + "_" +
FloatToStrF(Period,ffFixed,7,2) + "_" + FloatToStrF(Location,ffFixed,7,2);
which gives 2 decimals apiece...works, but would have nicer to have 1 or
0 decimals if that was the inpput, but .00 is okay too.
Barry wrote:
Quote
Hi All

I am using this line of text to setup a default SAVE-As file name.

AnsiString TempName = "Wave_" + FloatToStr(Height) + "_" +
FloatToStr(Period) + "_" + FloatToStr(Location);

As you can see these values that are brought into the filename are
floats and not integers and sometimes contain one or more digits after
the decimal.

This works fine for 1 decimal floats, but often with two decimal floats,
and without formating, the default name could be as follows:

Wave_2.5_13.4499998092651_63.5400009155273.txt

How do I limit the decimals to 2, so as to retain neat filenames.

Normally I use
sprintf(tmpstr,"Wave_%4.2f_%4.1f_%5.2f",Height,Period,Location);

to format these floats to a string, but this method creates white spaces
infront of each number in the string, so does not work in a filename.

Any suggestions?

Thanks

Barry




 

Re:Truncating decimals in use of Floats in a Filename

On Fri, 29 Feb 2008 10:02:40 +0200, Barry < XXXX@XXXXX.COM >wrote:
Quote
sprintf(tmpstr,"Wave_%4.2f_%4.1f_%5.2f",Height,Period,Location);
... this method creates white spaces
infront of each number in the string, so does not work in a filename.
Can't you prevent whitespace by specifying leading zeros via left
filling? e.g. -
sprintf(tmpstr,"Wave_%04.2f_%04.1f_%05.2f",Height,Period,Location);
- Wayne
- Wayne A. King
XXXX@XXXXX.COM
 

{smallsort}

Re:Truncating decimals in use of Floats in a Filename

Hi Barry
Barry says:
Quote
I eventually went with

AnsiString TempName = "Wave_" + FloatToStrF(Height,ffFixed,7,2) + "_" +
FloatToStrF(Period,ffFixed,7,2) + "_" + FloatToStrF(Location,ffFixed,7,2);

which gives 2 decimals apiece...works, but would have nicer to have 1 or
0 decimals if that was the inpput, but .00 is okay too.
if You can do with 0 decimals You can just cast the numbers to ints:
String TempName = "Wave_" + String((int)Height) + "_" +
String((int)Period) + "_" + String((int)Location);
Quote
>Normally I use
>sprintf(tmpstr,"Wave_%4.2f_%4.1f_%5.2f",Height,Period,Location);
>
if thats what You like most, why not just remove the blanks
in the string before using it as a filename
int t = 1, L = TempName.Length();
for(int i = 1; i <= L; ++i)
{
if(TempName[i] != ' ')
TempName[t++] = TempName[i];
}
TempName.SetLength(t-1);
Kind regareds
Asger
 

Re:Truncating decimals in use of Floats in a Filename

Can be zero decimals, but can be up to 2, so the fixed format 2 decimals
is the way to go I reckon.
Thanks for your tip on whitespace removal
Barry
Asger Joergensen wrote:
Quote
Hi Barry

Barry says:
>I eventually went with
>
>AnsiString TempName = "Wave_" + FloatToStrF(Height,ffFixed,7,2) + "_" +
>FloatToStrF(Period,ffFixed,7,2) + "_" + FloatToStrF(Location,ffFixed,7,2);
>
>which gives 2 decimals apiece...works, but would have nicer to have 1 or
>0 decimals if that was the inpput, but .00 is okay too.

if You can do with 0 decimals You can just cast the numbers to ints:

String TempName = "Wave_" + String((int)Height) + "_" +
String((int)Period) + "_" + String((int)Location);

>>Normally I use
>>sprintf(tmpstr,"Wave_%4.2f_%4.1f_%5.2f",Height,Period,Location);
>>

if thats what You like most, why not just remove the blanks
in the string before using it as a filename

int t = 1, L = TempName.Length();

for(int i = 1; i <= L; ++i)
{
if(TempName[i] != ' ')
TempName[t++] = TempName[i];
}

TempName.SetLength(t-1);

Kind regareds
Asger
 

Re:Truncating decimals in use of Floats in a Filename

"Barry" < XXXX@XXXXX.COM >wrote in message
Quote
How do I limit the decimals to 2, so as to retain neat filenames.
Have a look at FormatFloat()
Quote
Normally I use
sprintf(tmpstr,"Wave_%4.2f_%4.1f_%5.2f",Height,Period,Location);

to format these floats to a string, but this method creates
white spaces infront of each number in the string
Because you are telling it to do that. Read the documentation for sprintf()
more closely, specifically at the section about "Width Specifiers". It
tells you how it can insert zeros instead of spaces. For example:
AnsiString TempName;
TempName.sprintf("Wave_%04.2f_%04.1f_%05.2f", Height, Period, Location);
The result will be like this:
Wave_0002.5_0013.45_00063.54.txt
Quote
so does not work in a filename.
Spaces are allowed in filenames, and have been for 13 years now.
Gambit
 

Re:Truncating decimals in use of Floats in a Filename

"Asger Joergensen" < XXXX@XXXXX.COM >wrote in message
Quote
if thats what You like most, why not just remove the blanks
in the string before using it as a filename
Alternatively:
TempName = StringReplace(TempName, " ", "", TReplaceFlags() <<
rfReplaceAll);
Gambit
 

Re:Truncating decimals in use of Floats in a Filename

"Wayne A. King" < XXXX@XXXXX.COM >wrote in message
Quote
Can't you prevent whitespace by specifying leading zeros
via left filling?
Yes.
Gambit
 

Re:Truncating decimals in use of Floats in a Filename

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Wayne A. King" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Can't you prevent whitespace by specifying leading zeros
>via left filling?

Yes.
Remy -
Thanks for the confirmation, but it wasn't really needed.
I was asking Barry if there was any reason why *he* couldn't
use zero-padding with sprintf to accomplish what he wanted.
I know how the sprintf zero-fill specifier works with the
width specifier. That's why I included an example.
I guess I should eschew rhetorical questions in favour of
imperative assertions. (I was going to end that sentence
with a question mark, but thought better of it.) ;=)
- Wayne
 

Re:Truncating decimals in use of Floats in a Filename

Quote
Spaces are allowed in filenames, and have been for 13 years now.


Gambit
*smile*....yeah I guess, and so are "." but one tends to shy away from them
(old school and 8.3 format days I guess)
 

Re:Truncating decimals in use of Floats in a Filename

Quote
Thanks for the confirmation, but it wasn't really needed.
I was asking Barry if there was any reason why *he* couldn't
use zero-padding with sprintf to accomplish what he wanted.
I know how the sprintf zero-fill specifier works with the
width specifier. That's why I included an example.
I did not know u could.
Many thanks for the tips.
Clearly I tend to re-use old methods too much.....comes from spending very
limited time on app development, and then going back to see how I did it
last time.
Thanks for help, most appreciated.
Barry