How to use 'js float to int' in JavaScript

Every line of 'js float to int' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
39export function ToInt(stringOrFloatVal) { return parseInt(stringOrFloatVal); }
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

156function convert_floatToInt16(val){
157 var floatView = new Float32Array(1);
158 var int32View = new Int32Array(floatView.buffer);
159
160 floatView[0] = val;
161 var x = int32View[0];
162
163 var bits = (x >> 16) & 0x8000; /* Get the sign */
164 var m = (x >> 12) & 0x07ff; /* Keep one extra bit for rounding */
165 var e = (x >> 23) & 0xff; /* Using int is faster here */
166
167 /* If zero, or denormal, or exponent underflows too much for a denormal
168 * half, return signed zero. */
169 if (e < 103) {
170 return bits;
171 }
172
173 /* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
174 if (e > 142) {
175 bits |= 0x7c00;
176 /* If exponent was 0xff and one mantissa bit was set, it means NaN,
177 * not Inf, so make sure we set one mantissa bit too. */
178 bits |= ((e == 255) ? 0 : 1) && (x & 0x007fffff);
179 return bits;
180 }
181
182 /* If exponent underflows but not too much, return a denormal */
183 if (e < 113) {
184 m |= 0x0800;
185 /* Extra rounding may overflow and set mantissa to 0 and exponent
186 * to 1, which is OK. */
187 bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
188 return bits;
189 }
190
191 bits |= ((e - 112) << 10) | (m >> 1);
192 /* Extra rounding. An overflow will set mantissa to 0 and increment
193 * the exponent, which is OK. */
194 bits += m & 1;
195 return bits;
196}; //end convert_floatToInt16()

Related snippets