Velocity NumberTools formatter - velocity

I need to format a Number with Velocity using commas as decimal separator an dot as thousand separator.
I checked for other answer, but I could find only this:
How to use Velocity NumberTool for big currency numbers
which is not useful for my purpose, since in case i want to use different styles i need to create different functions.
I also checked the Velocity documentation and use different type of patterns, none of them working for me:
context.put("numberTool", new NumberTool());
$numberTool.format('#.###,00', $val)
$numberTool.format('###.###,00', $val)
$numberTool.format('#.###,##', $val)
$numberTool.format('###.###,##', $val)
Can somebody help me and find a way using if possible the Velocity Number Format?
Thanks in advance.

All you have to do is change the locale of the NumberTool. For instance:
[java]
NumberTool numberTool = new NumberTool();
numberTool.setLocale(Locale.FRENCH);
context.put("numberTool", numberTool);
[vtl]
$numberTool.format('#,###.00', $val)
$numberTool.format('###,###.00', $val)
$numberTool.format('#,###.##', $val)
$numberTool.format('###,###.##', $val)

Else if you dont have access to change the locale you could do another trick thanks to String.
-- To get $50000.00 to be $50,000.00 do (we need to convert to double)
#set($NumberFormatter = "")
$NumberFormatter.format("$%,.2f",$convert.toDouble($someNumberWithDigits))
-- To get $50000 to be $50,000 and no decimals do (we need to convert to int)
#set($NumberFormatter = "")
$NumberFormatter.format("$%,d",$convert.toInteger($someNumberWithDigits))

Related

Does SQL have a number separator (like an underscore) to split up large number literals

I'm trying to make it easier to read some SQL code where we need to hardcode in some large numbers
I'd like to do something like this:
SELECT 3_800_000
Which, according to this post, is really treated like this:
SELECT 3 _800_000
SELECT 3 AS [_800_000]
JS allows numeric separators
let x = 1000000000000
let y = 1_000_000_000_000
console.log(x==y) // true
Also, C# added digit separators in 7.0 as well
// These are equivalent.
var bigNumber = 123456789012345678;
var bigNumberSplit = 123_456_789_012_345_678;
Is something similar possible in T-SQL?
Note: I'm not looking for a way to format the output, I'm looking for a way to make the source code easier to read for big numbers
The answer is, unfortunately (and as commenters pointed out, thanks folks), that this is not currently a feature of T-SQL.
I was not aware of those JS and C#, so thanks for that.

Java - Index a String (Substring)

I have this string:
201057&channelTitle=null_JS
I want to be able to cut out the '201057' and make it a new variable. But I don't always know how long the digits will be, so can I somehow use the '&' as a reference?\
myDigits substring(0, position of &)?
Thanks
Sure, you can split the string along the &.
String s = "201057&channelTitle=null_JS";
String[] parts = s.split("&");
String newVar = parts[0];
The expected result here is
parts[0] = "201057";
parts[1] = "channelTitle=null_JS";
In production code you chould check of course the length of the parts array, in case no "&" was present.
Several programming languages also support the useful inverse operation
String s2 = parts.join("&"); // should have same value like s
Alas this one is not part of the Java standard libs, but e.g. Apache Commons Lang features it.
Always read the API first. There is an indexOf method in String that will return you the first index of the character/String you gave it.
You can use myDigits.substring(0, myDigits.indexOf('&');
However, if you want to get all of the arguments in the query separately, then you should use mvw's answer.

ASP.NET Raw SQL Escape Comma with Decimal Type

Question is in the title, how can I escape commas of a given decimal? Do I really need to workaround: get the value, convert it to String, replace commas with dots?
Is there a simpler solution to this? Or is there a good library or function I can use for this?
EDIT: I know I can enter values by replacing commas with dots. I'm asking for internal functions or external libraries for escaping SQL queries, as StringEscapeUtils in Java.
Having re-read your post it looks to me like the best way to be sure is to change the culture to en-US before you run your SQL statement. This will change all commas to dots in numbers. So something like
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en-US");
It depends on the current culture. You're culture is using dot as a separator.
Consider this code where you provide a culture that uses comma as a separator:
var dec = "123,45";
decimal parsed;
decimal.TryParse(dec, NumberStyles.Any, new CultureInfo("es-ES"), out parsed);

How to format numeric field using dutch format in extjs?

i want to format number entered by user in dutch format. ie. use decimal Separator as , and thousand seperator as .
blur: function () {
Ext.util.Format.number(this.value, '000,000.00')
}
I want to format my numeric field on blur, the above code works fine, but
my requirement is to get a format like this- '000000.000,00'.
How to do this in extjs?
Quick and dirty, just set the thousandSeparator and decimalSeparator. It should work:
//Set these once, right after Ext.onReady
Ext.util.Format.thousandSeparator = '.';
Ext.util.Format.decimalSeparator = ',';
//Then this should work:
Ext.util.Format.number(12345.67, '0,000.00'); //output 12.345,67
Or even better, use the localization, so formats can be changed according to language requirement.
Side note:
The documentation wrote:
To allow specification of the formatting string using UK/US grouping characters (,) and decimal (.) for international numbers, add /i to the end. For example: 0.000,00/i
And from the comments in the source code
// The "/i" suffix allows caller to use a locale-specific formatting string.
// Clean the format string by removing all but numerals and the decimal separator.
// Then split the format string into pre and post decimal segments according to *what* the
// decimal separator is. If they are specifying "/i", they are using the local convention in the format string.
To me, it seems that it means a developer can use a specific format string "0.000,00" to format a given number, and not to mean a developer can use this specific format string to format a number into the format they want. They will still need to change the default separator setting.
Edit
Demo link: http://jsfiddle.net/chaoszcat/nbWwN/
I have this working for user entered values in a numberfield now.
As lionel pointed out, this is needed:
// set this once after Ext.onReady
Ext.util.Format.thousandSeparator = '.';
Ext.util.Format.decimalSeparator = ',';
Then change your handler to this:
blur: function(field) {
field.setRawValue(Ext.util.Format.number(field.getValue(), '0.000,00/i'));
}
You should also include this config on your numberfield:
decimalSeperator: ','
It will allow users to type in their own decimal symbols.
Working Example
Here is a working fiddle of this, using a numberfield.
A word of warning
Ext.form.field.Number does not support formatting, the blur handler I gave above will work totally fine if the user edits the field and then does not go back into it to edit it again, if he refocuses the field it will validate and try to correct the thousands markers into decimals.
If you are using this field to post data back to the server it will send it back in the format that is displayed (with thousand seperators), I don't know if that was what you were going for.
If you simply want formatted numbers you should do what you're trying to do above but with a textfield. That way it won't reconfigure your thousands as decimals.
If you want all the functionality of a numberfield (spinners, min/max validation, step increments, etc) you will have to take a look at extending the numberfield class, here is a user extension that already exists and which is almost exactly what you needed, but it includes a currency symbol, it would fairly easy to take that out.

lua variable in pattern match

Im just wondering if it is possible to put a variable in a pattern match in Lua. Like something similar to the following:
var = "hello"
pattern = string.match(datasource, "(var)%s(a%+)")
The reason I need to do this is because the variable "var" will change periodically. (it will be in a loop)
Cheers in advance
Lua doesn't handle string interpolation inside of the quotes. Instead, you'll need to concatenate the parts with the var as a var reference and the rest as quote strings.
"("..var..")%s(a%+)" starts with a "(" as a string literal, concatenates the variable, then finishes off the rest of the string with a string literal.
Use "("..var..")%s(a%+)" instead.
I needed the same thing I think, a variable in a pattern match, but the above solution didn't work for me. I'm posting my solution in case it helps someone, didn't find anything else on the net like it.
I read a ': ' delimited file (name: tel) and want to search by name in the file and have the name and telephone number as answer.
local FileToSearch = h:read'*a' -- Read all the file
var = io.read() -- ask the name
string.gmatch(FileToSearch, ''..var..': '..'%d+') -- search for name, concatenate with number