|
Вопрос # 2 173/ вопрос решён / |
|
Доброго времени суток, уважаемые эксперты! Подскажите пожалуйста, как в Delphi (7) преобразовать данные типа real в string и обратно. Заранее благодарен.
 |
Вопрос задал: AlphA (статус: Посетитель)
Вопрос отправлен: 7 декабря 2008, 21:23
Состояние вопроса: решён, ответов: 3.
|
Ответ #1. Отвечает эксперт: min@y™
TryStrToFloat()
Ну и FloatToStrF(). Самый оно.
Приложение: Переключить в обычный режим- Converts a floating point value to a string, using a specified Format, Precision, and Digits.
-
- Unit
-
- SysUtils
-
- Category
-
- Floating point conversion routines
-
- Delphi syntax:
-
- function FloatToStrF(Value: Extended; Format: TFloatFormat; Precision, Digits: Integer): string;
overload;
- function FloatToStrF(Value: Extended; Format: TFloatFormat; Precision, Digits: Integer; const
FormatSettings: TFormatSettings): string; overload;
-
- C++ syntax:
-
- extern PACKAGE AnsiString __fastcall FloatToStrF(Extended Value, TFloatFormat Format, int Precision,
int Digits);
- extern PACKAGE AnsiString __fastcall FloatToStrF(Extended Value, TFloatFormat Format, int Precision,
int Digits, const TFormatSettings FormatSettings);
-
- Description
-
- FloatToStrF converts the floating-point value given by Value to its string representation.
-
- The Value parameter is the value to convert.
- The Precision parameter specifies the precision of the given value. It should be 7 or less for
values of type Single, 15 or less for values of type Double, and 18 or less for values of type
Extended.
- The Digits and Format parameters together control how the value is formatted into a string. For
details, see the description of TFloatFormat.
-
- If the given value is a NAN (not-a-number), the resulting string is 'NAN'. If the given value is
positive infinity, the resulting string is 'INF'. If the given value is negative infinity, the
resulting string is '-INF'.
-
- The first form of FloatToStrF is not thread-safe, because it uses localization information contained
in global variables. The second form of FloatToStrF, which is thread-safe, refers to localization
information contained in the FormatSettings parameter. Before calling the thread-safe form of
FloatToStrF, you must populate FormatSettings with localization information. To populate
FormatSettings with a set of default locale values, call GetLocaleFormatSettings.
 |
Ответ отправил: min@y™ (статус: Доктор наук)
Время отправки: 7 декабря 2008, 21:41
Оценка за ответ: 5
|
Ответ #2. Отвечает эксперт: Николай Рубан
Здравствуйте, AlphA!
Есть стандартные функции FloatToStr() и StrToFloat().
var x:real;
s:string;
begin
s:=FloatToStr(x); //переводим real в строку
//или
s:=Format('%f',[x]); //переводим real в строку
x:=StrToFloat(s); //переводим строку в real
end;
Good Luck!
 |
Ответ отправил: Николай Рубан (статус: 10-ый класс)
Время отправки: 7 декабря 2008, 21:44
Оценка за ответ: 5
|
Ответ #3. Отвечает эксперт: Anderson N.N
Здравствуйте, AlphA!
Вот функция которая кроме перевода строки из текста в строку проверяет возможность преобразования,
и в случае неудачи возвращает заданную вами константу(ее значение выбирают такой какой ваша строка принимать не должна
например -1000000)
она необходима когда вы пытаетесь перевести содержимое скажем Edit1 в число т.к. пользователь может ввести туда строку.
 |
Ответ отправил: Anderson N.N (статус: 2-ой класс)
Время отправки: 7 декабря 2008, 22:06
Оценка за ответ: 5
|
Мини-форум вопроса
Всего сообщений: 0.
31 января 2011, 19:23: Статус вопроса изменён на решённый (изменил модератор Ерёмин А.А.): Автоматическая обработка (2 и более ответов с оценкой 5)
Чтобы оставлять сообщения в мини-форумах, Вы должны авторизироваться на сайте.
|