RDC11040: Str of a number between -1 and 1
Severity: Warning · Family: RDC (Rubberduck Core analyzers) ·
Raised by: NumberTextAnalyzer
Str or Str$ is given a number that can be between -1 and 1: a Single, Double, Currency or Variant value, or a literal such as 0.5.
VBA writes such a number with no zero before its decimal point: Str(0.5) is " .5" and Str(-0.25) is "-.25".
MS-VBAL §6.1.2.3.1.20
writes the number as let-coercion to String does, with the zero: " 0.5". RD-VBA writes it as Microsoft Office VBA does, and this warning marks code
whose text depends on that difference.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
| The text is shown to a user. | MsgBox "Rate:" & Str(rate), with rate a Double |
Use Format$(rate, "0.0#"), which writes the digits the format asks for. |
| The text is read back by code that expects a zero. | Print #1, Str(ratio), read by a program that parses 0.5 |
Write the number with Format$ or CStr, which write the zero. Both use the regional decimal separator. |
| The value is never a fraction. | Str(total), with total a Double that holds whole numbers |
Declare it Long, or convert it first: Str(CLng(total)). |
See also
- RD-VBAL §2.6.4 Rubberduck Core Diagnostics
- RD-VBAL §6.1.2.3 Conversion Module
RDC11041:Strof anErrorvalueRDC11042:Write #of a number between -1 and 1
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca