How to parse and format an integer with zero decimal places in GWT? - gwt

Using GWT NumberFormat, I need to have a decimal format that basically accepts no decimal point character and zero decimal places. For example, 123 should be valid but 123.4 and 123.9 should be rejected. Also, rounding of decimal values into nearest integer is not an option.
I thought the following would work, but it does not:
double val = NumberFormat.getFormat("#0").parse(str);
I definitely need it to support GWT i18n formatting, such as "," separators for large numbers. The input "str" is for example the argument coming to a Parser.parse(text) method, similar to the one in IntegerParser. IntegerParser does not validate zero decimal places properly and only rounds the value rather than rejecting numbers with decimal point.
Any ideas?

Use IntegerParser.
It uses the following method under the covers:
(int) Math.rint(NumberFormat.getDecimalFormat().parse(object.toString()));
UPDATE:
Also, you can use LongBox instead of TextBox.

The way I finally solved my problem was by explicitly looking for decimal separator in the input string before trying to parse it using NumberFormat. Here is the code:
final String DECIMAL_SEPARATOR =
LocaleInfo.getCurrentLocale().getNumberConstants().decimalSeparator();
if (str.contains(DECIMAL_SEPARATOR))
throw new ParseException("Invalid integer value (" + str + ")", 0);
return (int) Math.rint(NumberFormat.getDecimalFormat().parse(str));
I am still interested in finding a better way to do it using NumberFormat.

Related

Talend: Converting a string to BigDecimal

I am trying to convert a string number from one MySQL table to another.
I have used the following on many occasions, however it does not seem to be working in this instance and I am unsure as to why. The string it is converting is 50,000.00.
With that formula in tMap it produces the following error:
When I look at the code on 3031:
So something is just not functioning as I expect. Any help would be great.
In regex syntax, "$" indicates the end of the string. In your regex, you are trying to remove any character after the end of the string which is not a number or a dot, which won't work, so the "," is never removed from your string, resulting in a conversion error.
You can do this:
new BigDecimal(row1.Trade_Amount.replaceAll("[^\\d.]", ""))

Cutting off decimals in Year conversion in Crystal Syntax

I'm printing the following year as a string in a report but it prints as 2,018.00. How do I have it print as a four digit year string without decimals or the comma? The Truncate() didn't seem to work.
CStr (Year({Date}) + 1)
You can either omit the CStr-function and set the number format on the formatting tab or, if the formula needs to return a string, you can use the arguments of the CStr- or ToText-function (which are equivalent).
Either set the second argument to define the number format:
CStr(Year({Date}) + 1, "####")
Or
Set the second and third argument to set the number of decimals to 0 and an empty string as thousands separator:
CStr(Year({Date}) + 1, 0, "")
What is happening is the Year() function converts the data into a Number, complete with thousands separator, decimal, and 2 significant digits after the decimal.
To get around this what I have found that works is to remove the CStr() function from your formula. This allows you to access the Formatting tab for a Number data type by right clicking the field and selecting Format Field. Then from the Number tab you can set the Style of the field to one of the styles that doesn't use a separator or decimal in the display.
If you are needing to concatenate this value with another string, then you can get a little more creative and use the LEFT() and REPLACE() functions like this.
Left(Replace(Cstr(Year({Date}) + 1), ",", ""), 4)

how to remove # character from national data type in cobol

i am facing issue while converting unicode data into national characters.
When i convert the Unicode data into national using national-of function, some junk character like # is appended after the string.
E.g
Ws-unicode pic X(200)
Ws-national pic N(600)
--let the value in Ws-Unicode is これらの変更は. getting from java end.
move function national-of ( Ws-unicode ,1208 ) to Ws-national.
--after converting value is like これらの変更は #.
i do not want the extra # character added after conversion.
please help me to find out the possible solution, i have tried to replace N'#' with space using inspect clause.
it worked well but failed in some specific scenario like if we have # in input from user end. in that case genuine # also converted to space.
Below is a snippet of code I used to convert EBCDIC to UTF. Before I was capturing string lengths, I was also getting # symbols:
STRING
FUNCTION DISPLAY-OF (
FUNCTION NATIONAL-OF (
WS-EBCDIC-STRING(1:WS-XML-EBCDIC-LENGTH)
WS-EBCDIC-CCSID
)
WS-UTF8-CCSID
)
DELIMITED BY SIZE
INTO WS-UTF8-STRING
WITH POINTER WS-XML-UTF8-LENGTH
END-STRING
SUBTRACT 1 FROM WS-XML-UTF8-LENGTH
What this code does is string the UTF8 representation of the EBCIDIC string into another variable. The WITH POINTER clause will capture the new length of the string + 1 (+ 1 because the pointer is positioned to the next position after the string ended).
Using this method, you should be able to know exactly how long second string is and use that string with the exact length.
That should remove the unwanted #s.
EDIT:
One thing I forgot to mention, in my case, the # signs were actually EBCDIC low values when viewing the actual hex on the mainframe
Use inspect with reverse and stop after first occurence of #

Crystal report issue with int to string conversion

I want to convert int to string and then concatenate dot with it. Here is the formula
totext({#SrNo})+ "."
It works perfectly but not what i want. I want to show at as
1.
but it shows me in this way
1.00.
it means that when i try to convert int to string it convert it into number with precision of two decimal zeros. Can someone tell me how can i show it in proper format. For information i want to tell you that SrNo is running total.
ToText(x, y, z, w) Function can use
x=The number to convert to text
y=The number of decimal places to include in result (optional). The value will be rounded to that decimal place.
z=The character to use as the thousands separator. If you don’t specify one, it will use your application default. (Optional.)
w=The character to use as the decimal separator. If you don’t specify one, it will use your application default. (Optional.)
Examples
ToText(12345.678) = > “12345.678″
ToText(12345.678,2) = > “12345.67″
ToText(12345.678,0) = > “12345″
You can try this :
totext({fieldname},0)
Ohhh I got the answer it was so simple.
totext takes 4 parameters
First parameter is value which is going to be converted
Second parameter is number of decimal previsions.
Third parameter is decimal separator. like (1,432.123) here dot(.) is third parameter.
Forth parameter is thousand separator. like (1,432) here comma(,) is forth parameter.
Example{
totext("1,432.1234",2) results 1,432.12
totext("1,432.1234",2,' " ') results 1,432"1234
totext("1,432.1234",2,' " ', ' : ') results 1:432,1234
}
Although i think this example may be not so good but i just want to give you an idea. This is for int conversion for date it has 2 parameters.
value to be converted and format of date.

Postgresql, treat text as numbers for getting MAX function result

Still didnt fix issue with dates written as strings here comes another problem.
I have text column where only numbers as writen (like text).
By using function MAX I get incorrect result because there 9 is bigger than 30.
Is here any inline function like VAL or CINT or something that I can compare and use textual data (only numbers) like numbers in queries like SELECT, MAX and other similar?
How than can look like in following examples:
mCmd = New OdbcCommand("SELECT MAX(myTextColumn) FROM " & myTable, mCon)
You need to use max(to_number(myTextColumn, '999999'))
More details are in the manual: http://www.postgresql.org/docs/current/static/functions-formatting.html
If all "numbers" are integers, you can also use the cast operator: max(myTextColumn::int)
If your text values are properly formatted you can simply cast them to double, e.g.: '3.14'::numeric.
If the text is not formatted according to the language settings you need to use to_number() with a format mask containing the decimal separator: to_number('3.14', '9.99')
To get the MAX works poterly you need to first convert your text field in numeric format
mCmd = New OdbcCommand("SELECT MAX(TO_NUMBER(myTextColumn, '99999')) FROM " & myTable, mCon)