How to represent large number without the E form in Scala - scala

I deal with numbers of this form 1.446267186999E7 and i want to represent them without E.
For example 1.446267186999E7 i want it to be 14462671.86999 .
How do i convert it to this form without getting the :
error: integer number too large.
Thanks for the helpers.

Try this:
BigDecimal(1.446267186999E7).toString
The BigDecimal.toString method will give you the string representation of the number in decimal form.

That is just a formatting problem if you store it as a double.
import java.text.DecimalFormat
val d: Double = 1.446267186999E7
val decimalFormat: DecimalFormat = new DecimalFormat("0.#####")
println(decimalFormat.format(d))
should give you 14462671.86999

You probably after BigDecimal. In terms of string formatting, look at the .format method, or printf

Related

Convert number to scientific notation and obtain exponent flutter

How can I convert a number to scientific notation and obtain the exponent? For example if I have 0.000000001 and want to convert it to 1e-9
Use the intl package and try it as following
final value = 0.000000025;
String res = NumberFormat.scientificPattern(Localizations.localeOf(context).languageCode).format(value);
print("Formatted value is $res");
EDIT
A better and cleaner approach, which does not require any additional package, is the method toStringAsExponential(). Here is how to use it
final value = 0.000000025;
print(value.toStringAsExponential());

How do I parse out a number from this returned XML string in python?

I have the following string:
{\"Id\":\"135\",\"Type\":0}
The number in the Id field will vary, but will always be an integer with no comma separator. I'm not sure how to get just that value from that string given that it's string data type and not real "XML". I was toying with the replace() function, but the special characters are making it more complex than it seems it needs to be.
is there a way to convert that to XML or something that I can reference the Id value directly?
Maybe use a regular expression, e.g.
import re
txt = "{\"Id\":\"135\",\"Type\":0}"
x = re.search('"Id":"([0-9]+)"', txt)
if x:
print(x.group(1))
gives
135
It is assumed here that the ids are numeric and consist of at least one digit.
Non-regex answer as you asked
\" is an escape sequence in python.
So if {\"Id\":\"135\",\"Type\":0} is a raw string and if you put it into a python variable like
a = '{\"Id\":\"135\",\"Type\":0}'
gives
>>> a
'{"Id":"135","Type":0}'
OR
If the above string is python string which has \" which is already escaped, then do a.replace("\\","") which will give you the string without \.
Now just load this string into a dict and access element Id like below.
import json
d = json.loads(a)
d['Id']
Output :
135

Get character from a string in Scala but keep the result as a string?

Imagine that I wanted to take the characters from a string in Scala but have the toInt conversion to behave as it would on a string instead of as on a character.
To illustrate the following code behaves like so:
"0".toInt // results in 0
"000".charAt(0).toInt // results in 48
I'd like a version of the second line that would also result in 0. I have a solution like the following:
"000".charAt(0).toString.toInt // results in 0
But I wonder if there is a more direct or better way?
You can use asDigit:
val i: Int = "000".charAt(0).asDigit
You can do:
"000".substring(0, 1).toInt
But I'm not sure it's more "direct" than "000".charAt(0).toString.toInt

double.parse(str) result is off by orders of magnitude

I have a weird problem in converting a string to double in .NET 3.5. Here is my code:
dbl = double.Parse(str);
When str is string with a simple double like "5.67" the result for dbl is 567.0.
I'd guess this is localisation issues and you need to use the overload that specifies a format provider.
The issue is likely that it is expecting , as a decimal separator and . as a thousand separator (and thus ignoring it in effect).
Example to reproduce possible issue:
string input = "5.67";
Console.WriteLine(Double.Parse(input, new CultureInfo("en-gb")));
Console.WriteLine(Double.Parse(input, new CultureInfo("de-de")));
This outputs:
5.67
567
I'm just editing Chris's answer:
value = "5.67";
double out;
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(Double.TryParse(value, style, culture, out number)?number:0);

how to change display in gtk.spinbutton

I have a gtk.spinbutton and I want to set the digits according to the locale
format.
like say I have a locale installed hungarian so my decimal separator is
'.'(dot) and thousand separator is ','(comma) eg: my spinbutton value is
1131191 so after the user focus out of the gtk.spinbutton my value should
convert to 11,311.91 . the conversion is made by me but I am not able to set
it to gtk.spinbutton either using set_text / set_value method.
Any help is appreciated !
Thanks
Formatting a SpinButton can be done by handling the output signal.
locale.setlocale(locale.LC_ALL, '')
def output(spin):
digits = int(spin.props.digits)
value = spin.props.value
text = locale.format('%.*f', (digits, value), True)
spin.props.text = text
return True
spin.connect('output', output)
If you also want to let users enter values in the localised format (e.g. let the user type "1,000" instead of "1000"), handle the input signal.
def input(spin, new_value):
text = spin.props.text
try:
value = locale.atof(text)
except ValueError:
return -1
p = ctypes.c_double.from_address(hash(new_value))
p.value = value
return True
spin.connect('input', input)
(This code is longer than it should be because PyGTK does not properly wrap input, hence the ctypes hack. It's just parsing the text and then assigning the numeric value to a pointer location.)
Credits: The ctypes hack and digits formatting are inspired by Tim Evans's post in the PyGTK mailing list.