Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Problema arrotondamenti

    Ho la necessità di effettuare degli arrotondamenti su degli importi, mediante javascript perchè deve andare ad aggiornare i dati di un form in base a come variano gli altri senza ricaricare la pagina.
    Ho trovato girovagando per il web qualcosa che sembrava fare al caso mio, ovvero il codice che riporto sotto; il problema è che in alcune situazioni sbaglia gli arrotondamenti; ad esempio, se faccio:

    3*9.17 ottengo 27.51 come netto ma 33.90 come valore lordo (dovrebbe essere 33.01, iva al 20%)

    Lordo e netto son ottenuti secondo questo calcolo:

    var lordo = parseMoney((quantita*prezzo*aliqiva) - (((quantita*prezzo)/100)*sconto*aliqiva),'.');

    var netto = parseMoney((quantita*prezzo) - (((quantita*prezzo)/100)*sconto),'.');

    Spero qualcuno mi sappia dare delle dritte, sia che riguardino il problema su questo codice sia eventuali sistemi alternativi

    codice:
    function parseDec(val,places,sep) {
    
    	// This function takes two arguments:
    	//   (string || number)  val
    	//            (integer)  places
    	//             (string)  sep
    	// val is the numeric string or number to parse
    	// places represents the number of decimal
    	// places to return at the end of the parse.
    	// sep is an optional string to be used to separate
    	// the whole units from the decimal units (default: '.')
    
    	val = '' + val;
    		// Implicitly cast val to (string)
    	
    	if (!sep) {
    		sep = '.';
    		// If separator isn't specified, then use a decimal point '.'
    	}
    	
    	if (!places) { places = 0; }
    	places = parseInt(places);
    		// Make sure places is an integer
    	
    	if (!parseInt(val)) {
    		// If val is null, zero, NaN, or not specified, then
    		// assume val to be zero.  Add 'places' number of zeros after
    		// the separator 'sep', and then return the value.  We're done here.
    		val = '0';
    		if (places > 0) {
    			val += sep;
    			while (val.substring((val.indexOf(sep))).length <= places) {
    				val += '0';
    			}
    		}
    		return val;
    	}
    	
    	if ((val.indexOf('.') > -1) && (sep != '.')) {
    		val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
    			// If we're using a separator other than '.' then convert now.
    	}
    		
    	if (val.indexOf(sep) > -1) {
    		// If our val has a separator, then cut our value
    		// into pre and post 'decimal' based upon the separator.
    		pre = val.substring(0,val.indexOf(sep));
    		post = val.substring(val.indexOf(sep)+1);
    	} else {
    		// Otherwise pre gets everything and post gets nothing.
    		pre = val;
    		post = '';
    	}
    	
    	if (places > 0) {
    		// If we're dealing with a decimal then...
    		
    		post = post.substring(0,(places+1));
    			// We care most about the digit after 'places'
    		
    		if (post.length > places) {
    			// If we have trailing decimal places then...
    			
    			//alert (parseInt(post.substring(post.length - 1)));
    
    			if ( parseInt(post.substring(post.length - 1)) > 4 ) {
    				post = '' + Math.round(parseInt(post) / 10);
    				//post = '' + post.substring(0,post.length - 2) + (1/Math.pow(10,places));
    				//post = ('' + post.substring(0,post.length - 2)) + (parseInt(post.substring(post.length - 1)) + 1);
    			} else {
    				post = '' + Math.round(parseInt(post));
    			}
    		}
    		
    		if (post.length > places) {
    			post = '' + Math.round(parseInt(post.substring(0,places)));
    		} else if (post.length < places) {
    			while (post.length < places) {
    				post += '0';
    			}
    		}
    	
    	} else {
    
    		if (parseInt((post.substring(0,1))) > 4) {
    			pre = '' + (parseInt(pre) + 1);
    		} else {
    			pre = '' + (parseInt(pre));	
    		}
    		post = '';
    	}
    	
    	sep = (post.length > 0) ? sep : '';
    		// Should we use a separator?
    
    	val = pre + sep + post;
    		// Rebuild val
    
    	return val;
    }
    
    function parseMoney(val,sep) {
    
    	// Specialized version of parseDec useful for
    	// parsing money-related data.  Arguments:
    	//   (string || number)  val
    	//             (string)  sep
    	// val is the monetary value to be parsed,
    	// sep is an optional decimal separator (default: '.')
    	
    	return parseDec(val,2,sep);
    }
    
    function sepToDec(val,sep) {
    
    	val = '' + val;
    
    	if ((val.indexOf(sep) > -1) && (sep != '.')) {
    		val = val.substring(0,val.indexOf(sep)) + '.' + val.substring(val.indexOf(sep)+1);
    	}
    	
    	return val;
    }
    
    function decToSep(val,sep) {
    
    	val = '' + val;
    	sep = '' + sep;
    
    	if ((val.indexOf('.') > -1) && (sep.length > 0)) {
    		val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
    	}
    
    	return val;
    }

  2. #2
    nessuno che mi sappia dare una mano ?!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.