summaryrefslogtreecommitdiffstats
path: root/helpcontent2/source/text/sbasic/shared/03120301.xhp
blob: ce5ef8b9aa8c38f243c62d506e380a775fdd7b40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
-->

<meta>
  <topic id="textsbasicshared03120301xml" indexer="include" status="PUBLISH">
    <title id="tit" xml-lang="en-US">Format Function</title>
    <filename>/text/sbasic/shared/03120301.xhp</filename>
  </topic>
</meta>

<body>


<section id="format">
<bookmark xml-lang="en-US" branch="index" id="bm_id3153539">
  <bookmark_value>Format function</bookmark_value>
</bookmark>


<h1 id="hd_id3153539"><link href="text/sbasic/shared/03120301.xhp">Format Function</link></h1>
<paragraph id="par_id3156042" role="paragraph" xml-lang="en-US">Converts a numeric expression to a string, and then formats it according to the format that you specify.</paragraph>
</section>

<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<bascode>
<paragraph id="par_id3153527" role="bascode" xml-lang="en-US">Format(expression [, format As String]) As String</paragraph>
</bascode>

<section id="ValueParms">
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph id="par_id3149415" role="paragraph" xml-lang="en-US"> <emph>expression</emph>: Numeric expression that you want to convert to a formatted string.</paragraph>
<paragraph id="par_id3147531" role="paragraph" xml-lang="en-US"> <emph>format</emph>: String that specifies the format code for the number. If <literal>format</literal> is omitted, the Format function works like the %PRODUCTNAME Basic <literal>Str()</literal> function.</paragraph>

<embed href="text/sbasic/shared/00000003.xhp#functvalue"/>
<paragraph id="par_id3148474" role="paragraph" xml-lang="en-US">Text string.</paragraph>
</section>

<h2 id="hd_id3147561">Formatting Codes</h2>
<section id="codes">
<paragraph id="par_id3147265" role="paragraph" xml-lang="en-US">The following list describes the codes that you can use for formatting a numeric expression:</paragraph>
<paragraph id="par_id3153380" role="paragraph" xml-lang="en-US"> <emph>0:</emph> If <literal>expression</literal> has a digit at the position of the 0 in the <literal>format</literal> code, the digit is displayed, otherwise a zero is displayed.</paragraph>
<paragraph id="par_id3151210" role="paragraph" xml-lang="en-US">If <literal>expression</literal> has fewer digits than the number of zeros in the <literal>format</literal> code, (on either side of the decimal), leading or trailing zeros are displayed. If the <literal>expression</literal> has more digits to the left of the decimal separator than the amount of zeros in the <literal>format</literal> code, the additional digits are displayed without formatting.</paragraph>
<paragraph id="par_id3151176" role="paragraph" xml-lang="en-US">Decimal places in the <literal>expression</literal> are rounded according to the number of zeros that appear after the decimal separator in the <literal>format</literal> code.</paragraph>
<paragraph id="par_id3154123" role="paragraph" xml-lang="en-US"> <emph>#:</emph> If <literal>expression</literal> contains a digit at the position of the # placeholder in the <literal>format</literal> code, the digit is displayed, otherwise nothing is displayed at this position.</paragraph>
<paragraph id="par_id3148452" role="paragraph" xml-lang="en-US">This symbol works like the 0, except that leading or trailing zeroes are not displayed if there are more # characters in the <literal>format</literal> code than digits in the <literal>expression</literal>. Only the relevant digits of the <literal>expression</literal> are displayed.</paragraph>
<paragraph id="par_id3159150" role="paragraph" xml-lang="en-US"> <emph>.:</emph> The decimal placeholder determines the number of decimal places to the left and right of the decimal separator.</paragraph>
<paragraph id="par_id3159252" role="paragraph" xml-lang="en-US">If the <literal>format</literal> code contains only # placeholders to the left of this symbol, numbers less than 1 begin with a decimal separator. To always display a leading zero with fractional numbers, use 0 as a placeholder for the first digit to the left of the decimal separator.</paragraph>
<paragraph id="par_id3153368" role="paragraph" xml-lang="en-US"> <emph>%:</emph> Multiplies the <literal>expression</literal>by 100 and inserts the percent sign (%) where the <literal>expression</literal> appears in the <literal>format</literal> code.</paragraph>
<paragraph id="par_id3149481" role="paragraph" xml-lang="en-US"> <emph>E- E+ e- e+ :</emph> If the <literal>format</literal> code contains at least one digit placeholder (0 or #) to the right of the symbol E-, E+, e-, or e+, the <literal>expression</literal> is formatted in the scientific or exponential format. The letter E or e is inserted between the number and the exponent. The number of placeholders for digits to the right of the symbol determines the number of digits in the exponent.</paragraph>
<paragraph id="par_id3149262" role="paragraph" xml-lang="en-US">If the exponent is negative, a minus sign is displayed directly before an exponent with E-, E+, e-, e+. If the exponent is positive, a plus sign is only displayed before exponents with E+ or e+.</paragraph>
<paragraph id="par_id3148617" role="paragraph" xml-lang="en-US">The thousands delimiter is displayed if the <literal>format</literal> code contains the delimiter enclosed by digit placeholders (0 or #).</paragraph>
<paragraph id="par_id3163713" role="paragraph" xml-lang="en-US">The use of a period as a thousands and decimal separator is dependent on the regional setting. When you enter a number directly in Basic source code, always use a period as decimal delimiter. The actual character displayed as a decimal separator depends on the number format in your system settings.</paragraph>
<paragraph id="par_id3152887" role="paragraph" xml-lang="en-US"> <emph>- + $ ( ) space:</emph> A plus (+), minus (-), dollar ($), space, or brackets entered directly in the <literal>format</literal> code is displayed as a literal character.</paragraph>
<paragraph id="par_id3148576" role="paragraph" xml-lang="en-US">To display characters other than the ones listed here, you must precede it by a backslash (\), or enclose it in quotation marks (" ").</paragraph>
<paragraph id="par_id3153139" role="paragraph" xml-lang="en-US"> <emph>\ :</emph> The backslash displays the next character in the <literal>format</literal> code.</paragraph>
<paragraph id="par_id3153366" role="paragraph" xml-lang="en-US">Characters in the <literal>format</literal> code that have a special meaning can only be displayed as literal characters if they are preceded by a backslash. The backslash itself is not displayed, unless you enter a double backslash (\\) in the format code.</paragraph>
<paragraph id="par_id3155411" role="paragraph" xml-lang="en-US">Characters that must be preceded by a backslash in the format code in order to be displayed as literal characters are date- and time-formatting characters (a, c, d, h, m, n, p, q, s, t, w, y, /, :), numeric-formatting characters (#, 0, %, E, e, comma, period), and string-formatting characters (@, &amp;, &lt;, &gt;, !).</paragraph>
<paragraph id="par_id3145749" role="paragraph" xml-lang="en-US">You can also use the following predefined number formats. Except for <literal>"General Number"</literal>, all of the predefined format codes return the number as a decimal number with two decimal places.</paragraph>
<paragraph id="par_id3150113" role="paragraph" xml-lang="en-US">If you use predefined formats, the name of the format must be enclosed in quotation marks.</paragraph>
</section>

<h2 id="hd_id3149377">Predefined format</h2>
<section id="formats">
<paragraph id="par_id3154730" role="paragraph" xml-lang="en-US"> <emph>General Number:</emph> Numbers are displayed as entered.</paragraph>
<paragraph id="par_id3153158" role="paragraph" xml-lang="en-US"> <emph>Currency:</emph> Inserts a dollar sign in front of the number and encloses negative numbers in brackets.</paragraph>
<paragraph id="par_id3154490" role="paragraph" xml-lang="en-US"> <emph>Fixed:</emph> Displays at least one digit in front of the decimal separator.</paragraph>
<paragraph id="par_id3153415" role="paragraph" xml-lang="en-US"> <emph>Standard:</emph> Displays numbers with a thousands separator.</paragraph>
<paragraph id="par_id3150715" role="paragraph" xml-lang="en-US"> <emph>Percent:</emph> Multiplies the number by 100 and appends a percent sign to the number.</paragraph>
<paragraph id="par_id3153836" role="paragraph" xml-lang="en-US"> <emph>Scientific:</emph> Displays numbers in scientific format (for example, 1.00E+03 for 1000).</paragraph>
<paragraph id="par_id3153707" role="paragraph" xml-lang="en-US">A <literal>format</literal> code can be divided into three sections that are separated by semicolons. The first part defines the format for positive values, the second part for negative values, and the third part for zero. If you only specify one <literal>format</literal> code, it applies to all numbers.</paragraph>
</section>
  
<embed href="text/sbasic/shared/00000003.xhp#languageformat"/>
<embed href="text/sbasic/shared/00000003.xhp#errorcode"/>
<embed href="text/sbasic/shared/00000003.xhp#err5"/>

<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph id="par_idm1341556320" role="bascode" localize="false" xml-lang="en-US">Sub ExampleFormat</paragraph>
<paragraph id="par_idm1341555088" role="bascode" localize="false" xml-lang="en-US">    MsgBox Format(6328.2, "##,##0.00")</paragraph>
<paragraph id="par_idN107A2" role="bascode" xml-lang="en-US">    ' always use a period as decimal delimiter when you enter numbers in Basic source code.</paragraph>
<paragraph id="par_id3147339" role="bascode" xml-lang="en-US">    ' displays for example 6,328.20 in English locale, 6.328,20 in German locale.</paragraph>
<paragraph id="par_idm1341550752" role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
</bascode>
<section id="relatedtopics">
    <paragraph role="paragraph" id="par_id381513082126889" xml-lang="en-US"><link href="text/shared/01/05020301.xhp">Number format codes</link></paragraph>
    <paragraph role="paragraph" id="par_id161599082457466" localize="false"><embedvar href="text/sbasic/shared/00000003.xhp#stringfunctions"/></paragraph>
</section>

</body>
</helpdocument>