I have a Flutter project, in SharedPrefs, I'm storing a double id, from a Third-part API. (so I can only get it as a double)
The id from prefs is 43449716574226770.0
I have to parse this double to a String, I'm doing it like this:
String idString = id.toStringAsFixed(0)
What I'm getting is somehow: 43449716574226768 but I'd like it to be just 43449716574226770
I have tried .toInt().toString() too, with the same results.
The weirdest part is that it sometimes works, with other ids.
This number as long would be in a 6-byte range?
As double this with an exponent part, this might mean that the previous double (bit wise) would have a difference more than 1. Also see java Math.ulp.
This implies that using a double instead of a long maps some double IDs to the same "long" ID.
I am not sure this is the problem here, but it would explain a deviation of 2.
Can you please try this
double x=43449716574226770.0;
print(x.toString());
Make it id.round().toString(), it will work.
Related
I got a List of String. I am losing information (the dot) when I try to convert an entry to type Double. What am I doing wrong?
Dim list As New List(Of String)
Dim a As Double
list.Add("309.69686")
a = CDbl(list(0))
MsgBox(a)
'Output: 30969686
This happens because in your locale the separator for decimal numbers is probably not a point but something else (usually a comma).
You are using the old VB6 methods to convert this string to a double and this method (CDbl) has no way to use a different locale settings.
So in the most basic form you need to change that method to the native .NET methods
a = Double.Parse(list(0), CultureInfo.InvariantCulture)
Here we pass the information about what locale setting Parse should use in converting the input string to a double. And the InvariantCulture uses the point as separator.
Of course, you should consider that, if the input string is obtained from the user input, then you could face other problems (like invalid numeric strings). In this case you should not use double.Parse, but double.TryParse
If you have a German Windows, then the dot will be interpreted as thousands separator. You must specify the culture explicitly, if you need another behaviour.
Dim d = Double.Parse("309.69686", CultureInfo.InvariantCulture)
There seems to be some inconsistency with the way tryParse is used in Dart or I'm going about it a silly way, likely the latter.
When we use the int.tryParse statement, if we pass it 10.0 as a double, we would expect to get 10 which we do.
print(int.tryParse(10.0.toString())); ==> 10
If we pass it 10.0 as a string, it will return null.
print(int.tryParse('10.0')); ==> null
I find this a bit strange as I would have thought 10.0.toString() is equivalent to '10.0'.
Does anyone have an explanation?
The difference is between web compiler and native code.
When Dart is compiled to the web (to JavaScript), all numbers are JavaScript numbers, which effectively means doubles.
That means that the integer 10 and the double 10.0 is the same object. There is only on "10" value in JavaScript.
So, when you do toString on that value, it has to choose, and it chooses "10" over "10.0".
When run natively, the double 10.0 and the integer 10 are two different objects, and their toStrings are "10.0" and "10" respectively.
The int.tryParse function does not accept "10.0" as input, which is why it returns null.
So, when testing on the web (including dartpad.dev), int.tryParse(10.0.toString()) succeeds because the toString is "10", and when testing natively the same code gives null because the toString is "10.0".
10.0.toString() returns '10', not '10.0'. That's why you have different results. Here is just the same question. So, as I understand it, we have:
dart 10.0 parses as int on js target (and there is not such a big difference between int and double on js target, as said on the link);
dart 10.0 parses as double on dart VM, but toString returns the shortest possible text representation of that number, that is 10.
This seems to be a compiler behaviour. I can't find someone reporting this issue. Maybe when you have zeroes after the comma, the toString method will round it. If all zeroes after comma, then it will convert to int.
how should I define a function that takes in a string and returns the string in upper and lowercases according to even and odd indexing?
def myfunc(string):
for some in string:
if string.index%2==0:
I have written this much but I do not know what should I type now.
please help.
Here you can find the string methods Specially look at upper() and lower() which will be your friend here.
In my project i want to store a binary string (string like "010101010101") to memcached and then retrieve it back when needed,
it seems that i can store the string successfully to memcached as "get xxx" gives me a result similar to original one,
but when i use "memcached_get" function to get the result in my code, the return string is not the same to the original binary string,
the length of the return string is much shorter than the original one.
Who can tell my why, is it possible to store binary string to memcached?
Thank you and looking forward your replies~!
Daniel.
Hi Joachim and rekire,
I have solved the problem yet.
I used libmemcache c++ client in my code, the return of "memcache_get" call is char*, i just used string value = string(xxx) to convert the char* to a string.
But actually the binary string contains '\r\n' thus i just got the string before the first '\r\n' and lost the rest data, that's the key point to the problem.
Now i just used string.append(xxx, length) to get the whole binary sequence.
rekire, thanks for your tips, the cause of the problem is similar to your suggestion.
Thank you~!
I have an EditText object (et_travel) on my screen that's asking for miles traveled. I grab that data like this:
float travel = Float.parseFloat(et_travel.getText().toString());
if(travel > 40000){
I just discover that if someone puts 40000 in the EditText, everything works fine, but if they put 40,000 (adding a comma to the number), I force close on the float travel = ...statement.
How can I evaluate the number without having a problem from the user adding a comma?
Is this in Java? It appears to be, but I'm wondering if I'm mistaken. Regardless, I would suggest you remove all of the characters from the string that are not of a numeric type. A way to do this may be using a regular expression.
A way to do this in Java may be the following:
String input = et_travel.getText().toString();
input = input.replaceAll("[^0-9]", "");
float travel = Float.parseFloat(input);
...
This way, you strip anything that is a non-numeric value from the string first, and then attempt to do your work. Obviously do some error checking before this (like input is not null and such). One change that is needed however is that you may need to maintain the '.' character (if you're given non-integer values). This would require changing the first regex a bit.
Check here: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
What you need is some validation on the input. Before converting the string into a float parse the string. If there are any ','s then remove them. If there is just junk then reject the input, otherwise someone could put a word or anything else in the input and cause havoc in your program.
Check out
inputType to restrict user input
android:inputType="number"