Functie Format

Converteert een numerieke expressie naar een tekenreeks en formatteert het dan naar de gespecificeerde opmaak.

Syntaxis:


Format(expression [, format As String]) As String

Parameters:

expression: Numerieke expressie die u wilt omzetten naar een geformatteerde tekenreeks.

format: Tekenreeks die de formaatcode specificeert voor het getal. Indien format wordt weggelaten, dan werkt de functie Format function als de LibreOfficeDev Basic functie Str().

Geretourneerde waarde:

Tekenreeks.

Opmaakcodes

Een format code kan in drie delen worden verdeeld met puntkomma's. Er dan dan een andere opmaak gegeven worden voor respectievelijk positieve waarde, negatieve waardes en de waarde nul. Als er maar een deel wordt ingevuld in de format code, dan is dat van toepassing op alle getallen.

U kunt de landinstelling instellen die wordt gebruikt voor het beheren van de opmaak van getallen, datums en valuta's in LibreOfficeDev Basic in - Taalinstellingen - Talen. In basisformaatcodes wordt de decimale punt (.) altijd gebruikt als plaatsaanduider voor het decimaalscheidingsteken dat in uw taalinstelling is gedefinieerd en wordt vervangen door het overeenkomstige teken.

Hetzelfde geldt voor de landinstellingen voor datum-, tijd- en valuta-opmaak. De BASIC-notatiecode wordt geïnterpreteerd en weergegeven volgens uw landinstelling.

De codes die gebruikt kunnen worden voor het formatteren van een numerieke expressie zijn:

Code

Description

0

If expression has a digit at the position of the 0 in the format code, the digit is displayed, otherwise a zero is displayed.

Als de expression minder cijfers bevat dan het aantal nullen in de format code, (aan beide zijden van de decimaal), dan worden nullen vooraan en achteraan getoond, Als de expression meer cijfers heeft voor de komma dan het aantal nullen in de format code, dan worden die extra cijfers onopgemaakt getoond.

Decimale plaatsen in de expression worden afgerond naar het aantal nullen na het decimale scheidingsteken in de format code.

#

If expression contains a digit at the position of the # placeholder in the format code, the digit is displayed, otherwise nothing is displayed at this position.

Het symbool werkt als de 0, echter zonder dat de nullen vooraan en achteraan worden getoond als er meer # tekens in de format code voorkomen dan in de expression. Alleen relevante cijfers van de expression worden getoond.

. (period)

The decimal placeholder determines the number of decimal places to the left and right of the decimal separator.

Als de format code links van dit symbool alleen # placeholders bevat, dan staat er voor getallen kleiner dan 1 een decimaal scheidingsteken. Om in deze gevallen toch een nul voor het decimaal scheidingsteken te krijgen dient u als placeholder een 0 te gebruiken op de eerste plaats links van het decimaal scheidingsteken.

Het gebruik van de punt als scheidingsteken voor duizendtallen en decimalen is afhankelijk van de landinstellingen. Als u een getal direct in BASIC-broncode invoert, gebruik dan altijd een punt als decimaal scheidingsteken. Het feitelijke teken dat wordt weergegeven als decimaal scheidingsteken is afhankelijk van uw systeeminstellingen.

%

%: Vermenigvuldigt de expressionmet 100 en voegt het procentteken (%) toe waar de expression voorkomt in de format code.

E- E+ e- e+

E- E+ e- e+ : Als de format code minstens een placeholder voor een cijfer (0 or #) bevat rechts van het symbool E-, E+, e-, or e+, dan wordt de expression opgemaakt in de de wetenschappelijke ook wel de exponentiële notatie. De letter E of e komt te staan tussen het getal en de exponent. Het aantal placeholders voor cijfers rechts van het symbool bepaald het aantal cijfers in de exponent.

Als de exponent negatief is, wordt een minteken vlak voor een exponent met E-, E+, e-, e+ geplaatst. Als de exponent positief is wordt een plusteken alleen voor exponenten met E+ of e+ geplaatst.

- + $ ( ) space

- + $ ( ) space: Een plus (+), minus (-), dollar ($), spatie of haakjes in de format code worden getoond als tekst.

\

Om andere tekens dan die hier opgesomd werden weer te geven, moet u het laten voorafgaan door een backslash (\), of het tussen aanhalingstekens plaatsen (" ").

\ : De backslash geeft aan dat het volgende teken in de format code dat teken zelf is en geen teken met een speciale betekenis voor de opmaak.

Tekens in de format code die een speciale betekenis hebben kunnen dus alleen als tekst worden getoond door er een backslash voor te zetten. De backslash wordt uiteraard niet getoond, om een backslash te tonen dient u een dubbele backslash (\\) in de de opmaakcode te zetten.

Tekens die moeten worden voorafgegaan door een backslash in de opmaakcode om te worden weergegeven als letterlijke tekens zijn de datum- en tijdopmaaktekens (a, c, d, h, m, n, p, q, s, t, w, y, /, :), numerieke opmaaktekens (#, 0, %, E, e, komma, punt) en tekenreeksopmaaktekens (@, &, <, >, !).


Voorgedefinieerde opmaak

U kunt ook een al gedefinieerde opmaak voor getallen gebruiken. Met uitzondering van de "Algemene getalopmaak" tonen al die opmaakcodes het getal als een getal met twee cijfers achter de komma.

Als u voorgedefinieerde opmaak gebruikt, moet de naam van de opmaak tussen aanhalingstekens staan.

Code

Description

"<"

Convert expression to lower case

">"

Convert expression to upper case.

"c" or "General Date"

Returns the numeric expression in short date format, optionally with "H:MM:SS AM/PM". If expression is a string, returns the string.

"n"

Returns the minute of the numeric expression, with 1 or 2 digits.

"nn"

Returns the minute of the numeric expression with two digits.

"w"

Returns the week day of the numeric expression. 1 is Sunday and 7 is Saturday.

"General Number"

Returns the numeric expression with 12 digits (0.############).

"Currency"

Returns the numeric expression in the currency of the locale.

"Fixed"

Returns the numeric expression with 2 decimal places (0.00).

"Standard"

Returns the numeric expression with thousands separators and 2 decimals (@0.00).

"Percent"

Returns the numeric expression as percent value (0.00%).

"Scientific"

Returns the numeric expression in scientific notation (#.00E+00);

"Yes/No"

Returns "Yes" if the numeric expression is not equal to zero, "No" otherwise. "Yes" and "No" are localized.

"True/False"

Returns "True" if the numeric expression is not equal to zero, "False" otherwise. "True" and "False" are localized.

"On/Off"

Returns "On" if the numeric expression is not equal to zero, "Off" otherwise. "On" and "Off" are localized.

"Long Date" or "dddddd"

Returns the numeric expression in system long date format, and depends on the locale.

"Medium Date"

Returns the numeric expression in date format DD-MMM-YY, and depends on the locale.

"Short Date" or "ddddd"

Returns the numeric expression in system short date format, and depends on the locale.

"Long Time" or "ttttt"

Returns the numeric expression in system long time format, and depends on the locale("H:MM:SS AM/PM").

"Medium Time"

Returns the numeric expression in system medium time format, and depends on the locale (HH:MM AM/PM)

"Short Time"

Returns the numeric expression in system short time format, and depends on the locale (HH:MM).


Foutcodes:

5 Ongeldige aanroep van procedure

Voorbeeld:


Sub ExampleFormat
    MsgBox Format(6328.2, "##,##0.00")
    ' gebruik altijd een punt als decimaal scheidingsteken als u getallen invoert in BASIC-broncode.
    ' geeft bijvoorbeeld 6,328.20 weer in Engelse landinstellingen en 6.328,20 in Nederlandse.
msgbox Format("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", "<") ' returns abcdefghijklmnopqrstuvwxyz1234567890, digits not affected.
msgbox Format("abcdefghijklmnopqrstuvwxyz1234567890", ">") ' returns ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890, digits not affected.
msgBox Format(12345.6,"c") ' returns the number in date format as 18/10/33 2:24:00 PM in German locale.
msgBox Format(12345.6,"General Date") ' Same as "c".
msgbox format(12345.004,"n") ' returns 5
msgbox format(12345.004,"nn") ' returns 05
msgbox format(12345.6,"w") ' returns 4 (Wednesday).
msgbox format(log(123),"General Number") ' returns 4,812184355372 in German locale
msgbox format(123456.7890,"Fixed") ' 123456.79
msgbox format(123456.1234,"Fixed") ' 123456.12
msgbox format(123456.7890,"Standard") '123,456.79
msgbox format(123456.1234,"Standard") '123,456.12
msgbox format(12.3456,"Percent") ' 1234.56%
msgbox format(0.123456,"Percent") '12.35%
msgbox format(123,"Yes/No") ' returns localized "Yes"
msgbox format(0,"Yes/No") ' returns localized "No"
msgbox format(-1,"True/False") ' returns localized "True"
msgbox format(123,"On/Off") ' returns localized "On"
msgbox format(45756.73,"Long Date") ' returns Wednesday, April 9, 2025 in your locale
End Sub