Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:41:56+00:00 2026-06-15T13:41:56+00:00

I have some simple javascript which I want to use to determine if a

  • 0

I have some simple javascript which I want to use to determine if a number needs to be rounded.

Example: User enters 1.2346 and the number is rounded to 1.235 and a message should be displayed to the user informing them that the number was rounded.
Rounding the number isn’t the issue, but showing the error message to the user is. I need to find the number of digits after the decimal point.

I use the following code to retrieve the decimal places off of a string. I then count the length of the variable to get the decimal places:

var dp = field_value - Math.floor(field_value);

However, When I test this I enter 1.23 for the value of field_value. When I check the value of field value it is indeed 1.23. When I check the value of Math.floor(field_value) it is indeed 1. But then I check the value of dp and it turns out to be 0.22999999999999998

Why does this subtraction not work the way it is expected?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:41:57+00:00Added an answer on June 15, 2026 at 1:41 pm

    Problem is given by the fact that floating numbers have a finite representation, take a look here and you will understand how and why.

    If problem with roundings is just for printing that number you could use something like sprintf for JS that allows you to format float output as you nee.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data in My controller which i want to use in Javascript
I have a simple .html page which uses Javascript to display some popups. Now
Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello
I have some simple JQuery / Javascript to perform some simple logic for all
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have some JavaScript, which is generated by PHP code, using a template. The
I have a simple javascript that uses $.ajax() from JQuery which works great for
I have a bunch of javascript functions which depend on some server-side constants (such
I have an external javascript file that I want to use to collect the
I want to do something very simple in javascript. I have a main.js file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.