10 examples of 'javascript convert float to int' in JavaScript

Every line of 'javascript convert 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
39export function ToInt(stringOrFloatVal) { return parseInt(stringOrFloatVal); }
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()
114function floatToFix(f)
115{
116 return parseInt(f*(1<<16));
117}
18function Float (value) {
19 return Number(value)
20}
237function roundToFloat(float) {
238 r[0] = float;
239 var float_r = r[0];
240 return float_r;
241}
116function convertFloatToInt(value, waveBitsPerSample, signedBorder)
117{
118 return (waveBitsPerSample == 8) ? (value + 1.0) * signedBorder
119 :
120 value * signedBorder;
121}
104function floatToInt_(sample, args) {
105 return parseInt(
106 sample > 0 ? sample * args.newMax : sample * args.newMin, 10);
107}
3export function testIntToFloat(a: i32): f32 {
4 return reinterpretf(a);
5}
65function float(str) {
66 if (!str) {
67 return 0;
68 }
69 return parseFloat(str, 10);
70}
22function roundFloat(val) {
23 return Math.floor(val * 100) / 100;
24}

Related snippets