Parsing number in flutter/dart - parsing

I'm trying to parse the string "78,74"(which is a valid number in Brazil's format) to double, but I'm getting Format Exception and I can't find any way to parse it... Already searched in intl docs but there's nothing helpful.
I don't want to replace the "," with "." because i think that it must be a way to parse it using CultureInfo
My code is like
String x = "78,74";
double d = double.tryParse(x)

You can use double d = NumberFormat('pt_Br').parse(x) from intl library. You will need to add the dependencies.

Localization support for parsing numbers can be found in package:intl. For this particular case, you're looking for the parse method from NumberFormat.

Related

Why do I get EXC_BAD_ACCESS when using %1$s for String(format:)

I have a localized string that looks like this:
String(format: "unable_to_reach".localized(), name) //name is a string. I have also tried just "x"
The key/value pair in the localize file looks like this:
"unable_to_reach" = "Unable to reach %1$s";
Now, sometimes this works, othertimes it crashes with the EXC_BAD_ACCESS error. Why is this? isn't %1$s supposed to be used for string values?
The format specifier %1$s is %s with a positional specifier of $1 inserted into it. %s is the format specifier for a null-terminated C string. If you instead pass it a Swift String, bad things will happen. Don't do that. (It will likely cause a buffer overflow if the Swift string does not contain any null bytes.)
You want %# (or %$1#, to preserve the positional specifier.)
See the document on string format specifiers for more information.
Edit:
BTW, you should think about using Swift string interpolation instead:
let unableToReach = "unable_to_reach".localized()
let final = "\(unableToReach) \(name)"
That is more "Swifty".
Note that if you want to use localized placeholders to allow for different word ordering in different languages, you really still need to use String(format:) and the %# (or %1# positional syntax) version.

How to add serializer in dart to convert iso 8601 to datetime object?

In dart I want to do this:
var s = "2018-11-23T04:25:41.9241411Z"; // string comes from a json but represented here for simplicity like this
var d = DateTime.parse(s);
but it throws a null.
Dart can't seem to parse iso 8601 date time formats. I've found a custom serializer called "Iso8601DateTimeSerializer" but how do I add it to my flutter app?
links: https://reviewable.io/reviews/google/built_value.dart/429#-
The instructions here only indicate adding it to dart using "SerializersBuilder.add" but I'm a newbie and cant find out how?
link:
https://pub.dartlang.org/documentation/built_value/latest/iso_8601_date_time_serializer/Iso8601DateTimeSerializer-class.html
The problem is that Dart's DateTime.parse only accepts up to six digits of fractional seconds, and your input has seven.
... and then optionally a '.' followed by a one-to-six digit second fraction.
You can sanitize your input down to six digits using something like:
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp(r"(\.\d{6})\d+"), (m) => m[1]);
Maybe the parse function should just accept more digits, even if they don't affect the value.
Just to add to Irn's answer. You need to add some escapes for the regex to work properly.
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(\\.\\d{6})\\d+"), (m) => m[1]);
You need to add it to the serializers builder.
Example:
#SerializersFor(models)
final Serializers serializers = (_$serializers.toBuilder()
..addPlugin(StandardJsonPlugin())
..add(Iso8601DateTimeSerializer()))
.build();

How to convert Float to String with out getting E in blackberry

Any way to convert Float to string with out getting E (exponent).
String str = String.valueOf(floatvalue);
txtbox.settext(str);
and i am using NumericTextFilter.ALLOW_DECIMAL in my textField which allow decimal but not E.
i am getting like this 1.3453E7 but i want it something like 1.34538945213 due to e i am not able to set my value in edit text.
so any way to get value with out e.
I'm not 100% sure I understand what number you're trying to format. In the US (my locale), the number 1.3453E7 is not equal to the number 1.34538945213. I thought that even in locales that used the period, or full stop (.) to group large numbers, you wouldn't have 1.34538945213. So, I'm guessing what you want here.
If you just want to show float numbers without the E, then you can use the Formatter class. It does not, however, have all the same methods on BlackBerry that you might expect on other platforms.
You can try this:
float floatValue = 1.3453E7f;
Formatter f = new Formatter();
String str = f.formatNumber(floatValue, 1);
text.setText(str);
Which will show
13453000.0
The 1 method parameter above indicates the number of decimal places to show, and can be anything from 1 to 15. It can't be zero, but if you wanted to display a number without any decimal places, I would assume you would be using an int or a long for that.
If I have misunderstood your problem, please post a little more description as to what you need.
I'll also mention this utility class that apparently can be used to do more numeric formatting on BlackBerry, although I haven't tried it myself.
Try this:
Double floatValue = 1.34538945213;
Formatter f = new Formatter();
String result = f.format("%.11f", floatValue);
Due to the floating point presentation in java, the float value 1.34538945213 has not the same representation as the double value 1.34538945213. So, if you want to get 1.34538945213 as output, you should use a double value and format it as shown in the example.

Blackberry: how to convert time from String in "hhhh:mm:ss.ss" to "mm:ss" formate

I have a String with time and I want to convert it to other format. I try several soulutions and some of them was a ridiculous as convert string to char array and find numbers to colons etc. But I fail in that and I haven't enough time to found way by themselves.
Can you give me a solution? This convertaion not usual but probably you have got a solution.
Thanks.
The format "hhhh:mm:ss.ss" seems unusual. But if you use any standard date format then you can use following code segments:
SimpleDateFormat sd = new SimpleDateFormat("hh:mm");
String time = sd.formatLocal(HttpDateParser.parse("2010-03-27 09:45:10"));
But if you prefer raw string processing then you can use some like this:
String input = "hhhh:mm:ss.ss";
String output = input.substring(input.indexOf(':') + 1, input.indexOf('.'));

Parsing a text file with different delimiters which contains nnumeric values

I have a text file which read:
config<001>25
23<220>12
.....
how can i parse so that i need only the values config,001(to be converted into integer after extracting using strtok or any ohter methods please suggest), and 25(to be converted into integer) seperately. i tries strtok its not working as the way i need. Please help me.
Use LINQ 2 SQL to import the file on the delimiters and then use something like AutoMapper to do the mapping of fields to say specific objects with specific types.
I did this exact thing in another project and it works great.
Based on the mention of strtok I'm guessing that you're using C or C++. If you're using C++, I'd probably handle this by creating a ctype facet that treats < and > as white space, which will make the parsing trivial (infile >> string >> number1 >> number2;).
If you're using C, you can use the scan-set conversion with scanf, something like: sscanf(line, "%[^<] %d> %d", string, &number1, &number2);

Resources