3

So basically the scenario is we have initial value 0.00

if the user enter any value like 2 than the decimal value should be 0.02

if the user enter any value like 3 than the decimal value should be 0.23 (appending to previous value of 0.02)

if the user enter any value like 5 than the decimal value should be 2.35 (appending to previous value of 0.23)

so if the user has entered 12345 it should be 123.45 .

I want something like this

enter image description here

Adnan Ashraf
  • 111
  • 8

5 Answers5

2

It seems like you are asking to divide a number by ten, but I may not understand the question. Code is below:

function d() {n=prompt("enter your number"); alert(n/100);}
button {
position: absolute;
top: 50%;
left: 50%;
}
<button onclick = "d();">move decimal</button>
Maze
  • 33
  • 3
0

You should treat your input as a string, push the number to the end of it, then convert back to number.

 let strValue = currentValue.toFixed(2).replace(".", ""); // Remove the decimal
 strValue = (parseInt(strValue, 10) * 10 + digit).toString(); // Shift left and add the new digit
 return parseInt(strValue, 10) / 100; // Convert back to a number with two decimal places
};

An example:

let currentValue = 0.00;
currentValue = addDigitToRight(currentValue, 2); // 0.02
currentValue = addDigitToRight(currentValue, 3); // 0.23
currentValue = addDigitToRight(currentValue, 4); // 2.34

Zenb0t
  • 44
  • 5
0

Here it is managed to work with if last digits are 00;

function decimalUtil(num){
  return (num/100).toFixed(2);
}

console.log(decimalUtil(1200)); //12.00
console.log(decimalUtil(1222)); //12.22
console.log(decimalUtil(12)); //0.12
console.log(decimalUtil(12225588)); //122255.88
Jerry
  • 1,005
  • 2
  • 13
-1

Here's a ReactJS implementation in TypeScript:

import { ChangeEvent, FC, useState } from "react"

const DecimalizedInput: FC = () => {
  const [value, setValue] = useState(0)

  /**
   * Decimalizes the value
   * @param {string} targetValue
   * @returns {number}
   */
  const decimalize = (targetValue: string) => {
    const currentValue = parseFloat(targetValue)

    /**
     * For handling value deletion
     */
    if (currentValue < value) return currentValue / 10
    /**
     * For handling value addition
     */
    return currentValue * 10
  }

  const onValueChange = (e: ChangeEvent<HTMLInputElement>) => {
    const targetValue = e.target.value
    /**
     * Ignoring non-numeric characters
     */
    if (targetValue && !targetValue.match(/^\d+(\.\d+)?$/)) return
    /**
     * Decimalizing the value
     */ else if (targetValue) setValue(decimalize(targetValue))
    /**
     * Resetting the value
     */ else setValue(0)
  }

  return (
    <div>
      <input
        value={
          // For displaying the value with 2 decimal places
          value.toFixed(2)
        }
        onChange={onValueChange}
      />
    </div>
  )
}

export default DecimalizedInput

You can get the code here as well.

-3

Here's an example implementation in Python:

    input_str = str(user_input)
    if len(input_str) <= 2:
        input_str = '0' + input_str
    else:
        input_str = input_str[:-2] + '.' + input_str[-2:]
    return float(input_str)

That’s an example in python. I hope that answers your question!