Best Way to Parse Float

How do I parse a string to a float or int?

>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545

Best way to parse float?

Depends where the input is coming from.

If your input comes from the user, you should use the CultureInfo the user/page is using (Thread.CurrentThread.CurrentUICulture).

You can get and indication of the culture of the user, by looking at the HttpRequest.UserLanguages property. (Not correct 100%, but I've found it a very good first guess) With that information, you can set the Thread.CurrentThread.CurrentUICulture at the start of the page.

If your input comes from an internal source, you can use the InvariantCulture to parse the string.

The Parse method is somewhat easier to use, if your input is from a controlled source. That is, you have already validated the string. Parse throws a (slow) exception if its fails.

If the input is uncontrolled, (from the user, or other Internet source) the TryParse looks better to me.

How to parse float with two decimal places in javascript?

You can use toFixed() to do that

var twoPlacedFloat = parseFloat(yourString).toFixed(2)

How to manually parse a floating point number from a string

I would directly assemble the floating point number using its binary representation.

Read in the number one character after another and first find all digits. Do that in integer arithmetic. Also keep track of the decimal point and the exponent. This one will be important later.

Now you can assemble your floating point number. The first thing to do is to scan the integer representation of the digits for the first set one-bit (highest to lowest).

The bits immediately following the first one-bit are your mantissa.

Getting the exponent isn't hard either. You know the first one-bit position, the position of the decimal point and the optional exponent from the scientific notation. Combine them and add the floating point exponent bias (I think it's 127, but check some reference please).

This exponent should be somewhere in the range of 0 to 255. If it's larger or smaller you have a positive or negative infinite number (special case).

Store the exponent as it into the bits 24 to 30 of your float.

The most significant bit is simply the sign. One means negative, zero means positive.

It's harder to describe than it really is, try to decompose a floating point number and take a look at the exponent and mantissa and you'll see how easy it really is.

Btw - doing the arithmetic in floating point itself is a bad idea because you will always force your mantissa to be truncated to 23 significant bits. You won't get a exact representation that way.

What's the best way to parse a float value from []bytes?

You can easily use strconv.ParseFloat for this, just converting your []byte to a string first. This would surely have less overhead than creating a reader and scanning with a scanf-like function.

sb := []byte("3.1415")
s := string(sb)

f, err := strconv.ParseFloat(s, 64)
if err != nil {
panic("whoops!")
}

fmt.Printf("%f\n", f)

Output:

3.141500

How to convert string into float in JavaScript?

If they're meant to be separate values, try this:

var values = "554,20".split(",")
var v1 = parseFloat(values[0])
var v2 = parseFloat(values[1])

If they're meant to be a single value (like in French, where one-half is written 0,5)

var value = parseFloat("554,20".replace(",", "."));

Convert string to either integer or float in javascript

That's how I finally solved it. I didn't find any other solution than to add the variable type to the variable ...

var obj = { a: '2', b: '2.1', c: '2.0', d: 'text'};// Explicitly remember the variable typefor (key in obj) {  var value = obj[key], type;  if ( isNaN(value) || value === "" ) {    type = "string";  }  else {    if (value.indexOf(".") === -1) {      type = "integer";    }    else {      type = "float";    }    value = +value; // Convert string to number  }  obj[key] = {    value: value,    type: type  };}document.write("<pre>" + JSON.stringify(obj, 0, 4) + "</pre>");

How to convert string to float in javascript without parseFloat or casting?

You could use an implicid type casting with an unary plus +.

var string = "43.44",    number = +string;    console.log(number);console.log(typeof number);

Best/quickest way to convert to float

TryParse is the best way.

See: http://msdn.microsoft.com/en-us/library/system.single.tryparse.aspx



Related Topics



Leave a reply



Submit