[
https://issues.apache.org/jira/browse/JENA-967?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15227188#comment-15227188
]
ASF GitHub Bot commented on JENA-967:
-------------------------------------
Github user ales004 commented on a diff in the pull request:
https://github.com/apache/jena/pull/132#discussion_r58619008
--- Diff:
jena-arq/src/main/java/org/apache/jena/sparql/expr/nodevalue/XSDFuncOp.java ---
@@ -352,6 +352,60 @@ public static NodeValue javaSubstring(NodeValue
nvString, NodeValue nvStart, Nod
}
}
+ // expecting nvString = format | nvStart = value (int,float,
string,....)
+ public static NodeValue javaSprintf(NodeValue nvFormat, NodeValue
nvValue) {
+ try {
+ String formatForOutput = nvFormat.getString() ;
+ ValueSpaceClassification vlSpClass = nvValue.getValueSpace();
+ switch(vlSpClass){
+ case VSPACE_NUM:
+ NumericType type =
classifyNumeric("javaSprintf",nvValue);
+ switch(type) {
+ case OP_DECIMAL:
+ BigDecimal decimalValue = nvValue.getDecimal();
+ return
NodeValue.makeString(String.format(formatForOutput,decimalValue)) ;
+ case OP_INTEGER:
+ BigInteger integerValue = nvValue.getInteger();
+ return
NodeValue.makeString(String.format(formatForOutput,integerValue)) ;
+ case OP_DOUBLE:
+ Double doubValue = nvValue.getDouble();
+ return
NodeValue.makeString(String.format(formatForOutput,doubValue)) ;
+ case OP_FLOAT:
+ Float floatValue = nvValue.getFloat();
+ return
NodeValue.makeString(String.format(formatForOutput,floatValue)) ;
--- End diff --
Depending on the Locale: 1.23 can be printed as 1.23 or 1,23
> Suggested ARQ Extension function: afn:printf(...)
> -------------------------------------------------
>
> Key: JENA-967
> URL: https://issues.apache.org/jira/browse/JENA-967
> Project: Apache Jena
> Issue Type: New Feature
> Components: ARQ
> Reporter: Stuart Williams
> Priority: Minor
>
> There are situations where it would be useful to be able to format strings
> bound to variable. In particular I have wanted to create some zero-filled
> numeric strings.
> I've accomplished the task with a pair of BINDs as follows where {{?code}} is
> bound to a numeric string:
> {noformat}
> BIND( "000000" as ?zfill )
> BIND(
> concat(if(strlen(?code)>=strlen(?zfill),"",substr(?zfill,strlen(?code)+1)),?code)
> as ?reg_notation)
> {noformat}
> But it would have been nice to be able to use something less opaque like:
> {noformat}
> BIND( afn:printf("%06d",?code) as ?reg_notation)
> {noformat}
> So 'improvement'/'new feature' suggestion is an additional ARQ Extension
> function that exposes java {{printf}} method.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)