0

I'm working on an app. I'm using JavaScript to save values to a database. My database table has a column that holds a Decimal value. It works fine with some old C# code. In fact, in C#, I'd do this:

decimal? myValue = null;

decimal temp = 0;
if (Decimal.TryParse(myString, out temp)) {
  myValue = temp;
}

I understand that JavaScript only has a single Number type. However, because I'm saving my value to the database, how do I ensure that its a decimal? In C#, I know that a Float is basically a 32-bit value, a Double is basically a 64-bit value, and a Deciml is basically a 128-bit value. However, I'm not sure how to translate this to JavaScript.

Can anyone provide some insights?

Thanks!

user687554
  • 10,663
  • 25
  • 77
  • 138
  • http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric – Mariatta Jul 04 '14 at 17:23
  • 2
    You have to make the check in the language you use to talk to the database. If it's javascript you should not have to do any checks, since, as you said yourself, all numbers are floating point numbers. So, if you have use a language that has decimals, just convert the value you get from JS. – Felix Kling Jul 04 '14 at 17:24

1 Answers1

0

You would check for decimals in javascript like this:

var dec = 3.14;
if(typeof dec == "number" && (dec+'').indexOf('.')!=-1){
   var myvalue = dec;
}

Note that the above will fail on numbers such as 5.00 as noted by FelixKling as the decimals are lost when it is converted to String.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • 1
    So, 5.00 would not be considered a decimal? – Felix Kling Jul 04 '14 at 17:25
  • @FelixKling any probable solution? BTW thanks for bringing that to notice – Amit Joki Jul 04 '14 at 17:29
  • I think testing whether a number is a decimal or not is irrelevant to the OPs actual problem. If it was just about testing it in JS your answer makes sense, but it looks like the OP has to ensure the *data type* of the value, which depends on the language OP is using to interface with the DB. It feels like an XY question... – Felix Kling Jul 04 '14 at 17:33
  • Na, keep it until the OP provides more info. Also, that's just my opinion ;) – Felix Kling Jul 04 '14 at 17:35
  • @FelixKling will obey fb dev's orders, sir! :) – Amit Joki Jul 04 '14 at 17:36
  • Can you explain what you mean by 'XY' question? I believe this question is relevant. The reason I believe that is because my database expects certain types of data. For that reason, I need to ensure a string matches requirements for that type of data before trying to put it in the database. Thank you for your help. – user687554 Jul 04 '14 at 17:39
  • @user687554 you aren't being more clear.. You've tagged it both c# and javascript. But you can't update a database from javascript and you'll need C# to validate the data for you. If you want client side validation, then my answer holds true.. – Amit Joki Jul 04 '14 at 17:43
  • You can update a database from JavaScript via Node.js. – user687554 Jul 04 '14 at 17:49
  • @user687554: In that case you don't need to do anything because there is only one number data type in JS. If that's not accepted by the DB, then you are out of luck, since you can't convert the value to any other number data type. XY question means that you are having a problem X, think you need solution Y and ask about Y instead of explaining your original problem X. So, why are you even asking the question? Are you already experience problems with storing numbers from JS in the DB and if yes, what are those problems. If not, why do you even bother about it? – Felix Kling Jul 04 '14 at 18:35
  • If the check is really necessary, would this condition not suffice? `(typeof dec === "number" && parseFloat(dec).toString().indexOf(".") > -1))` – Clarice Bouwer Jul 04 '14 at 18:56
  • @kleinkie: That's not different than what Amit already posted and has the same problem: It tests whether the *string* representation of a number is a decimal. There is only one number data type in JS. You can't test for something that doesn't exist. – Felix Kling Jul 04 '14 at 19:32
  • Whoops you are right, I misunderstood the last part of the edit. – Clarice Bouwer Jul 04 '14 at 19:35