Discussion:
Truncating decimals in use of Floats in a Filename
(too old to reply)
Barry
2008-02-29 08:02:40 UTC
Permalink
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
Barry
2008-02-29 09:12:51 UTC
Permalink
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.
Post by Barry
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,
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
Asger Joergensen
2008-02-29 13:32:39 UTC
Permalink
Hi Barry
Post by Barry
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);
Post by Barry
Post by Barry
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
Barry
2008-02-29 14:27:02 UTC
Permalink
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
Post by Asger Joergensen
Hi Barry
Post by Barry
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.
String TempName = "Wave_" + String((int)Height) + "_" +
String((int)Period) + "_" + String((int)Location);
Post by Barry
Post by Barry
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
Remy Lebeau (TeamB)
2008-02-29 18:56:12 UTC
Permalink
Post by Asger Joergensen
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
Wayne A. King
2008-02-29 13:16:21 UTC
Permalink
Post by Barry
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
***@idirect.com
Remy Lebeau (TeamB)
2008-02-29 18:56:34 UTC
Permalink
Post by Wayne A. King
Can't you prevent whitespace by specifying leading zeros
via left filling?
Yes.


Gambit
Wayne A, King
2008-03-04 04:44:36 UTC
Permalink
Post by Wayne A. King
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
Barry
2008-03-12 15:13:34 UTC
Permalink
Post by Wayne A, King
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

Remy Lebeau (TeamB)
2008-02-29 18:54:47 UTC
Permalink
Post by Barry
How do I limit the decimals to 2, so as to retain neat filenames.
Have a look at FormatFloat()
Post by Barry
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
Post by Barry
so does not work in a filename.
Spaces are allowed in filenames, and have been for 13 years now.


Gambit
Barry
2008-03-12 15:11:24 UTC
Permalink
Post by Remy Lebeau (TeamB)
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)
Loading...