切换搜索
搜索
切换菜单
notifications
切换个人菜单
查看“模块:StripToNumbers/doc”的源代码
来自决策链云智库
更多操作
←
模块:StripToNumbers/doc
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{module rating|beta}} <!-- Categories go at the bottom of this page and interwikis go in Wikidata. --> == Usage == This module extracts very basic numeric data from the input, namely the first match for a contiguous simple number, which may include the negative sign and a decimal, but not (yet) any further complexity, such as exponents, variables, etc. Its primary function is accepting data like: * <code>70%</code> * <code>margin-left: 20px;</code> * <code>75.485&nbsp;Khz</code> and return the numeric portion of it so that it can be operated on arithmetically. Results for each string, respectively: * {{#invoke:StripToNumbers | main | 70% }} * {{#invoke:StripToNumbers | main | margin-left: 20px; }} * {{#invoke:StripToNumbers | main | 75.485 Khz }} == Use cases == * Converting layout table cell dimensions given in <code>em</code>, <code>px</code>, or <code>%</code> to the bare-number proportions used by CSS's <code>flex-grow</code> declaration (only works if the units on all the cells are the same; can't handle a mixture, e.g. of a fixed-width sidebar and relative-width main content area). * Converting sloppy template input generally (e.g. measurements with units attached when only the measurement is wanted, or to remove unwanted <code>"</code> and <code>;</code> characters and the like). * Auto-generating halved values, e.g. to aid in conversion from old-school HTML 4 <code>cellspacing=...</code> to modern CSS <code>td {padding: ...;}</code> on all sides. == Limitations (serious ones) == {{See also|Module:StripToNumbers/testcases}} * '''When imput may contain a {{code|1=..=..}} character, use {{para|1|}} when calling the template.''' This never does any harm. ::So: when input is <code>a=70%</code> use <code><nowiki>{{#invoke:StripToNumbers|main|1=a=70%}}</nowiki></code> → ::{{#invoke:StripToNumbers|main|1=a=70%}} * '''Otherwise,''' The input cannot contain the <code>=</code> character unless it is escaped as <code><nowiki>{{=}}</nowiki></code> or <code>&equal;</code>. ** There may be other characters than <code>=</code> that must be escaped. * At present, the module only does three things: *# It finds the first contiguous number in the input string, which may be preceded by <code>-</code> (the keyboard hyphen-minus character, not the formal unicode minus <code>−</code>, and may contain a decimal; it throws away everything else. *# It checks that the result is a valid number (i.e. not something like <code>1.2.3</code> or <code>1-2-3</code>, nor null; this test may well be redundant code at this point, but better safe than sorry. *# It optionally divides the number by two (in a separate function). : Feel free to expand it to do more things (and to do what it does more robustly if you find a way to break it). Please report problems on the talk page and ping regular editors of the module. It is safest in most cases to expand by adding functions rather than adding features to the <code>main</code> function. * It will drop trailing zeros from the end of decimal. It may be useful to add a feature to stop that behavior, so that it won't mess with currency formatting (this cannot reliably be done by removing the "is it really a number?" test, since invocation of the halving function will operate on the string as, and convert it to, a number not a string, and thus result in the truncation of mathematically redundant zeroes. * It cannot handle <code>=</code>-style character entities that are numeric (nor their hex equivalents), for obvious reasons, only named ones like <code>&equal;nbsp;</code> This can be addressed in future upgrade, surely, but should be done in a separate function, as stripping such input down to the ASCII character numbers may well be the desired use in a particular instance. == Invocation == Basic usage: * <code><nowiki>{{#invoke:StripToNumbers | main | </nowiki>{{var|input}}<nowiki> }}</nowiki></code> To divide the resulting value by two: * <code><nowiki>{{#invoke:StripToNumbers | halve | </nowiki>{{var|input}}<nowiki> }}</nowiki></code> Same as main but returns null if no numbers in string, rather than error (can be used as contains numeric function): * <code><nowiki>{{#invoke:StripToNumbers | mainnull | </nowiki>{{var|input}}<nowiki> }}</nowiki></code> ==See also== *[[Module:ConvertNumeric]] - convert numbers to English words, and between number formats (e.g. decimal to hex) <includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox|| <!-- Categories go here and interwikis go in Wikidata. --> [[Category:Modules for general use]] }}</includeonly>
本页使用的模板:
模板:Code
(
查看源代码
)
模板:Module rating
(
查看源代码
)
模板:Para
(
查看源代码
)
模板:See also
(
查看源代码
)
模板:Var
(
查看源代码
)
模块:Arguments
(
查看源代码
)
模块:Format link
(
查看源代码
)
模块:Hatnote
(
查看源代码
)
模块:Hatnote/styles.css
(
查看源代码
)
模块:Hatnote list
(
查看源代码
)
模块:Labelled list hatnote
(
查看源代码
)
模块:StripToNumbers
(
查看源代码
)
返回
模块:StripToNumbers/doc
。