OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLNumberFormatter.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#pragma once
12
13#include <string>
14#include <vector>
15#include "XLCellValue.hpp"
16
17namespace OpenXLSX {
18
19 // TokenType for formatting instructions
20 enum class XLFormatTokenType {
21 Literal, // Literal string/character (e.g., "-", "/", " ", text)
22 Year, // yy or yyyy
23 Month, // m, mm, mmm, mmmm, mmmmm
24 Day, // d, dd, ddd, dddd
25 Hour, // h, hh
26 Minute, // m, mm (resolved via context)
27 Second, // s, ss
28 AMPM, // AM/PM, A/P
29 DigitZero, // 0 (forced digit, zero padded)
30 DigitOpt, // # (optional digit)
31 Decimal, // . (decimal point)
32 Thousands, // , (thousands separator)
33 Percent, // % (multiply value by 100)
34 TextPlaceholder // @ (text placeholder)
35 };
36
37 // FormatToken represents a parsed chunk of the format string
40 std::string value; // Holds the original placeholder string or literal text
41 int count; // Number of characters in the placeholder (e.g., "yyyy" = 4)
42 };
43
44 // FormatSection represents a single section of a multi-section format string
46 std::vector<XLFormatToken> tokens;
47 bool isDateTime{false};
48 bool isNumeric{false};
49 bool hasPercent{false};
51 };
52
53 // XLNumberFormatter parses an Excel number format string and applies it to an XLCellValue
55 public:
60 explicit XLNumberFormatter(const std::string& formatString);
61
67 std::string format(const XLCellValue& value) const;
68
69 private:
70 std::string m_formatString;
71 std::vector<FormatSection> m_sections;
72
73 // Core parsing phase
74 void parse();
75 void parseSection(const std::string& sectionStr);
76
77 // Formatting subroutines
78 std::string formatDateTime(double excelDate, const FormatSection& section) const;
79 std::string formatNumeric(double number, const FormatSection& section, bool addMinusSign) const;
80 std::string formatText(const std::string& text, const FormatSection& section) const;
81 };
82
83} // namespace OpenXLSX
Class encapsulating a cell value.
Definition XLCellValue.hpp:89
std::string format(const XLCellValue &value) const
Applies the format to a given cell value.
Definition XLNumberFormatter.cpp:28
XLNumberFormatter(const std::string &formatString)
Constructor that pre-compiles (parses) the format string.
Definition XLNumberFormatter.cpp:24
Definition IXLCellProvider.hpp:24
@ Percent
Definition XLConditionalFormatting.hpp:80
XLFormatTokenType
Definition XLNumberFormatter.hpp:20
@ Day
Definition XLNumberFormatter.hpp:24
@ DigitZero
Definition XLNumberFormatter.hpp:29
@ Year
Definition XLNumberFormatter.hpp:22
@ Minute
Definition XLNumberFormatter.hpp:26
@ DigitOpt
Definition XLNumberFormatter.hpp:30
@ Month
Definition XLNumberFormatter.hpp:23
@ TextPlaceholder
Definition XLNumberFormatter.hpp:34
@ Hour
Definition XLNumberFormatter.hpp:25
@ Second
Definition XLNumberFormatter.hpp:27
@ Thousands
Definition XLNumberFormatter.hpp:32
@ AMPM
Definition XLNumberFormatter.hpp:28
@ Literal
Definition XLNumberFormatter.hpp:21
@ Decimal
Definition XLDataValidation.hpp:26
Definition XLNumberFormatter.hpp:45
int decimalPlaces
Definition XLNumberFormatter.hpp:50
bool isNumeric
Definition XLNumberFormatter.hpp:48
bool isDateTime
Definition XLNumberFormatter.hpp:47
bool hasPercent
Definition XLNumberFormatter.hpp:49
std::vector< XLFormatToken > tokens
Definition XLNumberFormatter.hpp:46
Definition XLNumberFormatter.hpp:38
XLFormatTokenType type
Definition XLNumberFormatter.hpp:39
int count
Definition XLNumberFormatter.hpp:41
std::string value
Definition XLNumberFormatter.hpp:40