请求修改SharePoint列表条件格式代码,实现百分比两位小数显示
Got it, let's tweak your code to show the numeric value as a percentage with two decimal places, while keeping the conditional cell highlighting working correctly. Here's the revised code:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "debugMode": true, "elmType": "div", "attributes": { "class": "=if(@currentField >= 0.5, 'sp-field-severity--good', if(@currentField >= 0.25,'sp-field-severity--warning', if(@currentField < 0.25,'sp-field-severity--blocked', '')))" }, "children": [ { "elmType": "span", "txtContent": "=@currentField.toString('P2')" } ] }
Key Changes Made:
- Fixed syntax error: Added the missing closing quote for the
classattribute (your original code was missing this, which would break the formatting entirely). - Formatted as percentage with 2 decimals: Changed the
txtContentto use@currentField.toString('P2')— SharePoint's column formatting natively supports this method to convert numeric values to percentage strings, withP2enforcing exactly two decimal places (e.g.,0.5becomes50.00%,0.333becomes33.30%). - Removed mismatched icon logic: Your original code had an icon section checking for text values like
'Scheduled', which doesn't align with a numeric percentage field. I removed that since it wouldn't function with your numeric values. If you want to add icons tied to the percentage ranges, you can re-add the span and adjust theiconNamelogic to use the numeric conditions (e.g.,=if(@currentField >= 0.5, 'CheckMark', if(@currentField >=0.25, 'Warning', 'Error'))).
How It Works:
- Values ≥ 50% get the green "good" highlight
- Values between 25% and 49.99% get the yellow "warning" highlight
- Values <25% get the red "blocked" highlight
- All values display as a percentage with exactly two decimal places
内容的提问来源于stack exchange,提问作者Szymek
相关产品推荐
相关产品推荐

