How to specify the number of decimal places in a double? - dart

How does one specify the number of decimal places when outputting a string ?
For example, say I have the following:
var root3 = 1.73205080757;
And wish to output to two decimal places, how do I format the string, similar to how one does so in Java ?

doubles in dart have a method toStringAsFixed() which is easy to use:
If you have :
var root3 = 1.73205080757;
You can just do:
print(root3.toStringAsFixed(2));
Output:
1.73

Related

extract number with seperated point in a string ruby

How to extract a point separated number format from a string?
for example a string like this
a = 'ProductX credit 1.000'
how to to get only the 1.000 from that string?
Thank you kindly
You can use method split by space in ruby
a = 'ProductX credit 1.000'
a.split(" ").last
Result
"1.000"
Input
a='ProductX credit 1.000'
Code
p a.rpartition(/\s/).last
Output
"1.000"

How to extract double from string in dart?

I have a string containing 3 or 4 double numbers. what's the best way to extract them in an array of numbers?
First you have to find the numerals. You can use a RegExp pattern for that, say:
var doubleRE = RegExp(r"-?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?");
Then you parse the resulting strings with double.parse. Something like:
var numbers = doubleRE.allMatches(input).map((m) => double.parse(m[0])).toList();

extract a letter from a string in Lua

i have the string price that has a value with a number in it. I have code that extracts the number, I need help to figure out how to have another string (pricechar) with only the "k" in it
price="1k"
--pricechar=...
pricenum=string.match(price,"%d+")
You can extract all non-numeric characters, similar to how you do it for numbers:
pricechar = string.match(price,"[^%d]+")
To get both values at the same time:
pricenum, pricechar = string.match(price,"(%d+)(.*)")

Swift, iOS: How to convert a string containing number and character (i.e ',' or ',') into number?

I have an double that i am converting using NSMassFormatter from kg to lb.
let massFormatter = NSMassFormatter()
var xyz = massFormatter.stringFromKilograms(10000.000)
// xyz "22,046.226 lb"
Now I want a way to extract the number from the string. Also if I change the Locale to say es (Spain) then the value becomes "10.000,000 kg" (It actually returns "10.000 kg", removing the decimal points for unknown reasons), but i want a way such that I can extract the number regardless of the locale. Is there any standard way? Like use a regrex or some function in NSNumberFormatter?
Thank you
There is no way to do that fully independent of locale. The main problem is that identical string will be interpreted differently depending on what locale it is run against.
Best solution will be to identify all the possible formats, define all possible formatters and try to get numberFromString: from each formatter - until the first one to obtain the correct result.
The other solution, if you're getting the data from user input, is to explain the correct format to users and provide them with instant validation - i.e. showing "incorrect format" error message. Some apps have used the UIKeyboardTypeNumberPad keyboard to restrict user, so that you'll have only numeric values.
Two keys to the problem, finding the localized units (the "kg") part in your example, and converting the string using localized grouping and decimal separators:
// convert mass to string
var lbs = massFormatter.stringFromKilograms(10000)
println("\(lbs)")
// get localized unit specifier and remove from formatted string
var units = massFormatter.unitStringFromKilograms(10000, usedUnit: nil)
if let range = lbs.rangeOfString(units) {
lbs.replaceRange(range, with: "")
}
// get number formatter and set it to use grouping separator (, or .)
let numberFormatter = NSNumberFormatter()
numberFormatter.usesGroupingSeparator = true
// get number back
var kg = numberFormatter.numberFromString(lbs)
println("\(kg)")

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.

Resources