I'm using GWT 2.0 and when I try to use NumberFormat to format a Double the results are not as expected:
NumberFormat format = NumberFormat.getFormat( "#.########" );
Double d = new Double("0.256281093911");
format.format(d);
formatted string: 0.02147484
As you can see the formatted value is wrong (this can be seen in the gwt showcase). Is this something related to the custom format I'm using (#.########)? or is this a bug in the GWT formatter?
If this is a bug, have someone found a workaround?
Thanks.
link text
you might try NumberFormat.getDecimalFormat. Also, it might have something to do with your browser's locale and/or it's decimal delimiter.
A Double is stored in binary format in java in general. Working with binary float values is in most cases not what you want - conversion between binary and decimal floats causes rounding errors.
Try "BigDecimal", which stores float values in DECIMAL format, not in binary format.
Sample code:
NumberFormat format = NumberFormat.getFormat("#.########");
format.format(new BigDecimal("0.256281093911"));
Related
let's say I have a DateTime object and I want to display it in the correct local format.
If I do the following on a German device I get this:
dateTime.toLocal().toString()
// Prints
2022-05-28 23:29:19.518
However, I would expect or desire more something like this for a German device: 28.5.2022 23:29:19
I know that I can format the DateTime but that would just be hardcoding it for a certain locale.
Weirdly enough all the solutions that I found for this on StackOverflow are either hardcoding the format or only apply to Dart, not Flutter.
What is the correct way to display a local datetime in Flutter?
You can use this package intl and localise dates like
var format = DateFormat.yMd('ar');
var dateString = format.format(DateTime.now());
Using the intl package which was mentioned here already, this has been working well for me so far:
DateFormat dateTimeFormat = DateFormat.jm(Localizations.localeOf(context).toString());
DateTime dt = DateTime.fromMicrosecondsSinceEpoch(entity.syncDateTime);
dateTimeFormat.format(dt);
To get outputs which are not yet supported I, for example, concat a ymd formatted DateTime string with a jm formatted DateTime string.
I was expecting Decimal to treat number like a String but instead I reproduced small rounding during testing.
let locale = Locale(identifier: "en_GB")
let price: String? = "1535132527181273627"
let decimal: Decimal? = price != nil ? Decimal(string: price!, locale: locale) : nil
XCTAssertEqual(decimal, 1535132527181273627.0)
The result bothers me:
XCTAssertEqual failed: ("Optional(1535132527181274000)") is not equal to ("Optional(1535132527181273497.6)") -
I tried your code and the result was:
XCTAssertEqual failed: ("Optional(1535132527181273627)") is not equal to ("Optional(1535132527181273497.6)")
using Xcode 9.4.1 on a MacBook Pro 2015
I think the problem is in the fact that Double is only able to deal with 15 decimal places. So the number 1535132527181273627.0 cannot be represented accurately. Take a look at Double precision - decimal places
This is a design problem of the Swift compiler.
It has been reported as SR-3317 but that was closed as duplicate of SR-920.
The problem is that the literal 1535132527181273627.0 is parsed as a Double literal (that is, with limited precision) even if the type should be Decimal. Using a String to initialize Decimal is the only workaround for now.
The solution is to redesign the built-in protocol ExpressibleByFloatLiteral to allow for longer numbers.
So it looks like the problem is in the test, not the code. Swift uses Double to store number from the test and it's not long enough, Decimal wasn't rounded but the number to compare in the test was.
Solution is to compare Decimal.
XCTAssertEqual(price, Decimal(string:"1535132527181273627"))
I am inserting dates that look like:
'19APR2014:08:42:32.123456'
I am interpreting their format as
'DDMONYYYY:HH24:MI:SS.FFFFFF'
Though I have not seen any times after 12:59:59 I am assuming a 24-hour clock. Hive does not seem to understand what I want to do:
HiveException: Error evaluating unix_timestamp(date_string,'DDMONYYYY:HH24:MI:SS.FFFFFF')
Any ideas what I am doing wrong or what might be wrong with my format string?
Have you tried ddMMMyyyy:HH:mm:ss.SSS? According to Hive manual a pattern string in function unix_timestamp(string date, string pattern) should comply to Java's SimpleDateFormat(see manual and javadocs).
I am using the jPlaton platform and I have a PlatonScript record that contains a date and a time field.
The date field is a integer like 20140526.
I want to convert it to a YYYY-MM-DD string. That it: 20140526 --> "2014-05-26"
The time field is a integer like 90903417.
I want to convert it to an HH:mm:ss string. That it: 90903417 --> "09:09:03"
Any ideas?
Thanks
The basic functions you need are:
ASDATE (for integer to string transformation)
example: #s_dateFromInteger# = (ASDATE:YYYY-MM-DD) #s_integerDate#
ASTIME (for long to string transformation)
example: #s_timeFromLong# = (ASTIME:HH:mm:ss) #s_longTime#
In these examples #s_dateFromInteger# and #s_timeFromLong# are the Strings you require.
s_integerDate is the classic integer date representation used by jPlaton (yyyyddmm).
s_longTime is the classic long time representation used by the platform (hhmmssSSS)
Don't forget that the date and time patterns used by Platonscript are the usual JAVA patterns, since Platonscript interpreter returns pure JAVA
For example, I have an NSDecimal myDecimal. Lets say it represents something like "-1234567.89"
How can I get a clean string representation of that decimal without any beautification / formatting? No localization? Floating point symbol = . and rest only numbers from 0 to 9, and eventually an - if it is negative? I need that string in strict technical manner. A number like 123456789876554432123456789.2231 should therefore not look like nice formatted "123,456,789,876,554,432,123,456,789.2231". You get the point right?
I don't want any formatting. I'm trying all day now to get that right but everything I find always has to do with formatting. So how'd you guys do that?
I don't believe there is such a thing as a "clean" string representation independent of specifying the locale. As many Europeans would point out, 123.45 should be written as 123,45 (using , instead of . for the decimal location). NSDecimalString() (which can be found in the Foundation Functions Reference) takes, as a second parameter a locale specification. If some locale uses the format you desire, pass that locale as the second parameter (see the Internationalization Guide for more info on locales).
Alternatively, you can use an NSNumberFormatter, which will give you more controll over the string representation.
For easy (localized) control use an NSNumberFormatter.
float number = 12.345;
NSString* numberString = [NSString stringWithFormat:#"%f", number];
That will give you consistent formatting regardless of the user's current locale.