I need to generate a large array of numbers ranging from 0.00 to say 10000000.00 i.e 0.01 0.02 0.03 ... 1.51 1.52 1.583 ... etc
After looking around, I tried this answer which works fine for integers but however not working for decimals
I need to generate a large array of numbers ranging from 0.00 to say 10000000.00 i.e 0.01 0.02 0.03 ... 1.51 1.52 1.583 ... etc
After looking around, I tried this answer which works fine for integers but however not working for decimals
You didn't specify a language, so I will use Python.
import numpy as np
numbers = np.arange(0, 10000000.01, 0.01)
# Print first 10 elements
print(numbers[:10])
# Print last 10 elements
print(numbers[-10:])
A simple way, try this:
let array = [];
for (let i = 0; i <= 1000000000; i++)
{
array.push((i * 0.01).toFixed(2));
}
In javascript, you can try:
let numbers = [];
for (let i = 0; i <= 10000000; i += 0.01) {
numbers.push(parseFloat(i.toFixed(2))); //you can use toFixed(3) too if you want
}
Please note that creating a very large array like this will consume a lot of memory and may lead to performance issue.
const arr = Array.from(new Array(10000000), (_, index) => (index / 100).toFixed(2));
console.log(arr);
With this code, you will get an array of the numbers described in the question, after the dot there will be 2 numbers: 0.00, 0.01 and so on.
Try the following
let array = [];
let val = 0.00;
do{
val += 0.01;
array.push(val);
}while(val<1000000000.00);
You didn't specify a language, so I will use Javascript.
To generate a large array of numbers ranging from 0.00 to 10000000.00, you can use a loop to create the array in JavaScript. Here's a sample code to achieve that:
function generateIncrementedNumbers(start, end, step) {
const result = [];
for (let i = start; i <= end; i += 0.01) {
result.push(i.toFixed(2));
}
return result;
}
const startNumber = 0.0;
const endNumber = 10000000.0;
const largeArray = generateIncrementedNumbers(startNumber, endNumber);
console.log(largeArray);
In Javascript
function generateNumberRange(start, end, decimalPlaces) {
const step = 1 / Math.pow(10, decimalPlaces);
const range = [];
for (let num = start; num <= end; num += step) {
range.push(parseFloat(num.toFixed(decimalPlaces)));
}
return range;
}
// Example usage:
const startValue = 0.00;
const endValue = 10000000.00;
const decimalPlaces = 2;
const resultArray = generateNumberRange(startValue, endValue, decimalPlaces);
console.log(resultArray);