fix removal of ranges
This commit is contained in:
parent
fc46b45386
commit
12db3c9e3c
@ -10,21 +10,15 @@ function convertFloatsToFractions(inputString) {
|
|||||||
// Define a helper function to check if a number is close to an integer
|
// Define a helper function to check if a number is close to an integer
|
||||||
const isCloseToInt = (num) => Math.abs(num - Math.round(num)) < 0.001;
|
const isCloseToInt = (num) => Math.abs(num - Math.round(num)) < 0.001;
|
||||||
|
|
||||||
// Iterate through the words and convert floats to fractions
|
// Function to convert a float to a fraction
|
||||||
const result = words.map((word) => {
|
const floatToFraction = (number) => {
|
||||||
const number = parseFloat(word);
|
|
||||||
|
|
||||||
if (!isNaN(number)) {
|
|
||||||
if (isCloseToInt(number)) {
|
|
||||||
return Math.round(number).toString();
|
|
||||||
} else {
|
|
||||||
let bestNumerator = 0;
|
let bestNumerator = 0;
|
||||||
let bestDenominator = 1;
|
let bestDenominator = 1;
|
||||||
let minDifference = Math.abs(number);
|
let minDifference = Math.abs(number);
|
||||||
|
|
||||||
for (let denominator = 1; denominator <= 10; denominator++) {
|
for (let denominator = 1; denominator <= 10; denominator++) {
|
||||||
const numerator = Math.round(number * denominator);
|
const numerator = Math.round(number * denominator);
|
||||||
const difference = Math.abs(number- (numerator / denominator));
|
const difference = Math.abs(number - numerator / denominator);
|
||||||
|
|
||||||
if (difference < minDifference) {
|
if (difference < minDifference) {
|
||||||
bestNumerator = numerator;
|
bestNumerator = numerator;
|
||||||
@ -32,66 +26,36 @@ function convertFloatsToFractions(inputString) {
|
|||||||
minDifference = difference;
|
minDifference = difference;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(bestDenominator == 1) return bestNumerator
|
|
||||||
|
if (bestDenominator == 1) return bestNumerator;
|
||||||
else {
|
else {
|
||||||
let full_amount = Math.floor(bestNumerator/bestDenominator)
|
let full_amount = Math.floor(bestNumerator / bestDenominator);
|
||||||
if(full_amount > 0) return `${full_amount}<sup>${bestNumerator - full_amount * bestDenominator}</sup>/<sub>${bestDenominator}</sub>`;
|
if (full_amount > 0)
|
||||||
|
return `${full_amount}<sup>${bestNumerator - full_amount * bestDenominator}</sup>/<sub>${bestDenominator}</sub>`;
|
||||||
return `<sup>${bestNumerator}</sup>/<sub>${bestDenominator}</sub>`;
|
return `<sup>${bestNumerator}</sup>/<sub>${bestDenominator}</sub>`;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
return word;
|
// Iterate through the words and convert floats to fractions
|
||||||
|
const result = words.map((word) => {
|
||||||
|
// Check if the word contains a range (e.g., "300-400")
|
||||||
|
if (word.includes('-')) {
|
||||||
|
const rangeNumbers = word.split('-');
|
||||||
|
const rangeFractions = rangeNumbers.map((num) => {
|
||||||
|
const number = parseFloat(num);
|
||||||
|
return !isNaN(number) ? floatToFraction(number) : num;
|
||||||
|
});
|
||||||
|
return rangeFractions.join('-');
|
||||||
|
} else {
|
||||||
|
const number = parseFloat(word);
|
||||||
|
return !isNaN(number) ? floatToFraction(number) : word;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Join the words back into a string
|
// Join the words back into a string
|
||||||
return result.join(' ');
|
return result.join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertFloatsToFractionsMath(inputString) {
|
|
||||||
// Split the input string into individual words
|
|
||||||
const words = inputString.split(' ');
|
|
||||||
|
|
||||||
// Define a helper function to check if a number is close to an integer
|
|
||||||
const isCloseToInt = (num) => Math.abs(num - Math.round(num)) < 0.001;
|
|
||||||
|
|
||||||
// Iterate through the words and convert floats to fractions
|
|
||||||
const result = words.map((word) => {
|
|
||||||
const number = parseFloat(word);
|
|
||||||
|
|
||||||
if (!isNaN(number)) {
|
|
||||||
if (isCloseToInt(number)) {
|
|
||||||
return Math.round(number)
|
|
||||||
} else {
|
|
||||||
let bestNumerator = 0;
|
|
||||||
let bestDenominator = 1;
|
|
||||||
let minDifference = Math.abs(number);
|
|
||||||
|
|
||||||
for (let denominator = 1; denominator <= 10; denominator++) {
|
|
||||||
const numerator = Math.round(number* denominator);
|
|
||||||
const difference = Math.abs(number- (numerator / denominator));
|
|
||||||
|
|
||||||
if (difference < minDifference) {
|
|
||||||
bestNumerator = numerator;
|
|
||||||
bestDenominator = denominator;
|
|
||||||
minDifference = difference;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(bestDenominator == 1) return bestNumerator
|
|
||||||
else{
|
|
||||||
let full_amount = Math.floor(bestNumerator/bestDenominator)
|
|
||||||
return full_amount + (bestNumerator - full_amount * bestDenominator)/ bestDenominator
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return word;
|
|
||||||
});
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function multiplyNumbersInString(inputString, constant) {
|
function multiplyNumbersInString(inputString, constant) {
|
||||||
return inputString.replace(/(\d+(?:[\.,]\d+)?)/g, match => {
|
return inputString.replace(/(\d+(?:[\.,]\d+)?)/g, match => {
|
||||||
const number = match.includes(',') ? match.replace(/\./g, '').replace(',', '.') : match;
|
const number = match.includes(',') ? match.replace(/\./g, '').replace(',', '.') : match;
|
||||||
@ -136,7 +100,7 @@ function apply_if_not_NaN(custom){
|
|||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
custom_mul = convertFloatsToFractions(custom)
|
custom_mul = convertFloatsToFractions(custom)
|
||||||
multiplier = convertFloatsToFractionsMath(custom)
|
multiplier = custom
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
Loading…
Reference in New Issue
Block a user