I have such a query: SELECT m, y, point, predprijatie, title, sum (prihod) FROM prihod GROUP BY m, y, point, predprijatie, prihod How to make it so that the field sum (prihod) is displayed in the money format?
- You repeat. [About floating point result format] [1]. [1]: hashcode.ru/questions/70479 - KiTE
3 answers
If we are talking about Delphi, then in order to convert a floating-point number into a monetary format, the Format()
function is sufficient.
Format('%2.2m', 1234.777); // Вернет 1 234,78 р. или 1 234,78 грн. или другой вариант
The monetary unit and its position after or in front of a number depends on the settings of the client’s operating system (Control Panel - Regional and Language Options).
If you need to register a hard version of the format, you can do so:
Format('%2.2n грн.', 1234.777); // Вернет 1 234,78 грн.
If the output of a number in a specific format is considered in the case of using a TDataSet data source (TQuery, TTable, etc.), then the most convenient option is to set the OnGetText handler for the required field in this DataSet.
Handler code might look like this:
procedure TForm1.Table1Field1_GetText(Sender: TField; var Text: String; DisplayText: Boolean); begin if DisplayText then begin Text := ''; if (Sender.AsVariant <> Null) then Text := Format('%2.2m', [Sender.AsFloat]) end; end;
The meaning of this handler is that the number in the data source remains a number, and it is displayed as a formatted string.
Data presentation is a matter of application. If we talk about Delphi, the TField class has the DisplayFormat and EditFormat properties for setting the display format and editing, respectively. You can do for example:
TFloatField(DataSet.Fields.FieldByName('FIELD_NAME')).DisplayFormat := '0.00, руб';
then in all components that will display your field, FIELD_NAME will be in this form. Details can be found in the help.
CAST(SUM(prihod) AS DECIMAL(20, 2))
20 and 2 - the number of characters before and after .
- Why it does not work. Those. does not display the result in the format "00.00" ... - Leonid
- And how does it lead? Perhaps, somewhere the value is still converted to a whole after being extracted from the database - oleg42
- Yes, frankly, it doesn’t change either ... the result is both integer and floating point. The fact is that this query is in the project of Delphi and the result of the query is output to the DBGrid. In the case when I displayed values in Label, I did this: FMain.Label24.Caption: = FloatToStrF (StrToFloat (uDatas.ADQPrihodRashodMainSUMprihod.Value), ffCurrency, 30, 2); But how do I format the result in a dbgrid, I don’t understand something. - Leonid